/
project_list.go
165 lines (151 loc) · 4.09 KB
/
project_list.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
153
154
155
156
157
158
159
160
161
162
163
164
165
package topics
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"sort"
"strconv"
"time"
"github.com/OnitiFR/barry/cmd/barry/client"
"github.com/OnitiFR/barry/common"
"github.com/c2h5oh/datasize"
"github.com/fatih/color"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
var projectListFlagBasic bool
var projectListFlagSize bool
// projectListCmd represents the "project list" command
var projectListCmd = &cobra.Command{
Use: "list [project]",
Short: "List all projects or all project's files",
// Long: ``,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
projectListFlagBasic, _ = cmd.Flags().GetBool("basic")
projectListFlagSize, _ = cmd.Flags().GetBool("size")
if projectListFlagBasic {
client.GetExitMessage().Disable()
}
if len(args) > 0 {
projectName := args[0]
call := client.GlobalAPI.NewCall("GET", "/project/files", map[string]string{
"project": projectName,
})
call.JSONCallback = projectFileListCB
call.Do()
} else {
call := client.GlobalAPI.NewCall("GET", "/project", map[string]string{})
call.JSONCallback = projectListCB
call.Do()
}
},
}
func projectListCB(reader io.Reader, headers http.Header) {
var data common.APIProjectListEntries
dec := json.NewDecoder(reader)
err := dec.Decode(&data)
if err != nil {
log.Fatal(err.Error())
}
// default (controller) sort is by name
if projectListFlagSize {
sort.Slice(data, func(i, j int) bool {
return data[i].SizeCountCurrent < data[j].SizeCountCurrent
})
}
if projectListFlagBasic {
for _, line := range data {
fmt.Println(line.Path)
}
} else {
if len(data) == 0 {
fmt.Printf("Currently, no projects exists.\n")
return
}
grey := color.New(color.FgHiBlack).SprintFunc()
strData := [][]string{}
for _, line := range data {
name := line.Path
if line.Archived {
name = grey(name)
}
strData = append(strData, []string{
name,
strconv.Itoa(line.FileCountCurrent),
datasize.ByteSize(line.SizeCountCurrent).HR(),
fmt.Sprintf("%.2f", line.CostCurrent),
})
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Path", "Files", "Size", "Cost"})
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.AppendBulk(strData)
table.Render()
}
}
func projectFileListCB(reader io.Reader, headers http.Header) {
var data common.APIFileListEntries
dec := json.NewDecoder(reader)
err := dec.Decode(&data)
if err != nil {
log.Fatal(err.Error())
}
if projectListFlagBasic {
for _, line := range data {
fmt.Println(line.Filename)
}
} else {
strData := [][]string{}
yellow := color.New(color.FgHiYellow).SprintFunc()
red := color.New(color.FgHiRed).SprintFunc()
for _, line := range data {
var name string
if line.Encrypted {
name = "🔒"
} else {
name = " "
}
name += line.Filename
maxExpire := line.ExpireRemote
container := "(local)"
if line.ExpireLocal.After(line.ExpireRemote) {
maxExpire = line.ExpireLocal
}
expire := maxExpire.Format("2006-01-02 15:04")
if time.Until(maxExpire) < time.Hour*24 {
expire = red(expire) // will expire in less than one week
} else if time.Until(maxExpire) < time.Hour*24*7 {
expire = yellow(expire)
}
if line.ExpiredLocal {
container = yellow(line.Container)
}
if line.Retrieved {
container = "(retrieved)"
}
strData = append(strData, []string{
name,
line.ModTime.Format("2006-01-02 15:04"),
datasize.ByteSize(line.Size).HR(),
expire,
container,
})
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "mtime", "Size", "Expire", "Container"})
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.AppendBulk(strData)
table.Render()
}
}
func init() {
projectCmd.AddCommand(projectListCmd)
projectListCmd.Flags().BoolP("basic", "b", false, "show basic list, without any formating")
projectListCmd.Flags().BoolP("size", "s", false, "sort by size (ascending)")
}