forked from google/skicka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls.go
152 lines (140 loc) · 3.7 KB
/
ls.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// ls.go
// Copyright(c)2014-2015 Google, Inc.
//
// This file is part of skicka.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package main
import (
"fmt"
"github.com/google/skicka/gdrive"
"google.golang.org/api/drive/v2"
"os"
"path/filepath"
"time"
)
func getPermissionsAsString(driveFile *drive.File) (string, error) {
var str string
if gdrive.IsFolder(driveFile) {
str = "d"
} else {
str = "-"
}
perm, err := getPermissions(driveFile)
if err != nil {
// No permissions are available if the file was uploaded via the
// Drive Web page, for example.
str += "?????????"
} else {
rwx := "rwx"
for i := 0; i < 9; i++ {
if perm&(1<<(8-uint(i))) != 0 {
str += string(rwx[i%3])
} else {
str += "-"
}
}
}
return str, nil
}
func ls(args []string) int {
// Parse command line arguments.
long := false
longlong := false
recursive := false
var argFilenames []string
for _, arg := range args {
if len(argFilenames) > 0 {
// After the end of command-line arguments, all subsequent args
// are treated as filenames, regardless of whether they match
// any of the flags.
argFilenames = append(argFilenames, arg)
} else if arg == "-l" {
long = true
} else if arg == "-ll" {
longlong = true
} else if arg == "-r" {
recursive = true
} else if arg[0] == '-' {
fmt.Printf("Usage: skicka ls [-l,-ll,-r] <drive path...>\n")
fmt.Printf("Run \"skicka help\" for more detailed help text.\n")
return 1
} else {
argFilenames = append(argFilenames, arg)
}
}
if len(argFilenames) == 0 {
argFilenames = append(argFilenames, "/")
}
errs := 0
for index, drivePath := range argFilenames {
drivePath = filepath.Clean(drivePath)
if len(argFilenames) > 1 {
fmt.Printf("%s:\n", drivePath)
}
// Get the files for the current path from Google Drive.
includeBase := false
mustExist := true
files, err := gd.GetFilesUnderPath(drivePath, recursive, includeBase,
mustExist)
if err != nil {
fmt.Fprintf(os.Stderr, "skicka: %s: %v\n", drivePath, err)
errs++
continue
}
// Sort the individual filenames returned.
sorted := files.GetSorted()
for _, f := range sorted {
printFilename := f.Path
if !recursive {
printFilename = filepath.Base(printFilename)
}
if gdrive.IsFolder(f.File) {
printFilename += "/"
}
if long || longlong {
synctime, _ := gdrive.GetModificationTime(f.File)
permString, _ := getPermissionsAsString(f.File)
if longlong {
md5 := f.File.Md5Checksum
if len(md5) != 32 {
md5 = "--------------------------------"
}
fmt.Printf("%s %s %s %s %s\n", permString,
fmtbytes(f.File.FileSize, true), md5,
synctime.Format(time.ANSIC), printFilename)
if debug {
fmt.Printf("\t[ ")
for _, prop := range f.File.Properties {
fmt.Printf("%s: %s, ", prop.Key,
prop.Value)
}
fmt.Printf("id: %s ]\n", f.File.Id)
}
} else {
fmt.Printf("%s %s %s %s\n", permString,
fmtbytes(f.File.FileSize, true),
synctime.Format(time.ANSIC), printFilename)
}
} else {
fmt.Printf("%s\n", printFilename)
}
}
if len(argFilenames) > 1 && index < len(argFilenames)-1 {
fmt.Printf("\n")
}
}
return errs
}