-
Notifications
You must be signed in to change notification settings - Fork 0
/
yard.go
123 lines (114 loc) · 2.68 KB
/
yard.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
package main
import (
"fmt"
"net/url"
"path/filepath"
"strings"
"github.com/Songmu/prompter"
pty "github.com/kr/pretty"
"github.com/urfave/cli"
)
var commandYard = cli.Command{
Name: "yard",
Aliases: []string{"y"},
Usage: "Control your yard",
ArgsUsage: "[options...] image_name",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "display, d",
Usage: "Show a detail fo the image config data",
},
cli.BoolFlag{
Name: "list, l",
Usage: "Show the list of all image config data in your yard",
},
cli.BoolFlag{
Name: "create, c",
Usage: "Create new image config data in your yard",
},
},
Action: yard,
}
func yard(c *cli.Context) {
if isV {
fmt.Println("dcenv yard ", c.Args())
}
if c.Bool("list") {
ListYardAll()
return
}
if len(c.Args()) < 1 {
fmt.Println("No image name.")
cli.ShowSubcommandHelp(c)
return
}
dname, _, _ := ParseImageTag(c.Args()[0])
if c.Bool("create") {
CreateNewYard(dname)
return
}
NewYardPackFromYard(dname).Disp()
return
}
func ListYardAll() {
// fname:=filepath.Join(envHome ,"image_yard",url.QueryEscape(dname)+".yml")
if filenames, err := filepath.Glob(filepath.Join(envHome, "image_yard", "*.yml")); err == nil {
for _, val := range filenames {
str, err := url.QueryUnescape(strings.TrimSuffix(filepath.Base(val), ".yml"))
if err != nil {
fmt.Println(err)
} else {
fmt.Println(str)
}
}
}
}
func (yd *Yard) Disp(i string) {
fmt.Printf("---[ %s ]---------\n", i)
fmt.Println(" Id :", yd.ID)
fmt.Println(" Owner :", yd.Owner)
// fmt.Println(" Createat:",yd.CreateAt)
fmt.Println(" Image :", yd.Image)
fmt.Println(" Brief :", yd.Brief)
fmt.Println(" Desc :", yd.Desc)
fmt.Println(" Pri :", yd.Pri)
for key, cnt := range yd.Config {
fmt.Println(" Config :", key)
fmt.Println(" Tag :", cnt.Tag)
fmt.Println(" Fake :", cnt.Fake)
pty.Printf(" Commands: %v\n", cnt.Commands)
fmt.Printf(" Script :\n%s\n", cnt.Script)
}
return
}
/*
func ListYardDetail(dname string) {
yp := NewYardPackFromYard(dname)
for i, yd := range yp {
yd.Disp(strconv.Itoa(i))
}
}
*/
func CreateNewYard(dname string) {
yp := NewYardPackFromYard(dname)
if len(yp) > 0 {
fmt.Printf("*** A file already exists. ***\n")
yp.Disp() //ListYardDetail(dname)
if ret := prompter.YN("Erase it?", false); !ret {
exit(1)
return
}
}
nyp := YardPack{}
y := Yard{
Image: dname, // string `json:"image,omitempty"`
Brief: "Brief " + dname, // string `json:"brief,omitempty"`
}
y.Config = make(ImagePack)
y.Config["bash"] = Image{}
y.Config["windows"] = Image{}
nyp.AddItem(y)
f := nyp.WriteToYard(dname)
fmt.Println("Edit:", f)
return
}