-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.go
176 lines (171 loc) · 4.17 KB
/
main.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
166
167
168
169
170
171
172
173
174
175
176
// Copyright (c) 2020-present Cloud <cloud@txthinking.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 3 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"errors"
"log"
"net/http"
"os"
"github.com/bitly/go-simplejson"
"github.com/urfave/cli/v2"
)
var version = "20240902"
func main() {
app := cli.NewApp()
app.Name = "nami"
app.Version = version
app.Usage = "A clean and tidy package manager."
app.Authors = []*cli.Author{
{
Name: "Cloud",
Email: "cloud@txthinking.com",
},
}
app.Copyright = "https://github.com/txthinking/nami"
app.Before = func(c *cli.Context) error {
if len(os.Args) == 3 && os.Args[1] == "install" && os.Args[2] == "nami" {
return nil
}
res, err := http.Get("https://api.github.com/repos/txthinking/nami/releases/latest")
if err != nil {
log.Println("Check nami version", err)
return nil
}
defer res.Body.Close()
j, err := simplejson.NewFromReader(res.Body)
if err != nil {
log.Println("Check nami version", err)
return nil
}
s, err := j.Get("tag_name").String()
if err != nil {
log.Println("Check nami version", err)
return nil
}
if s > ("v" + version) {
return errors.New("New version: " + s + ", please upgrade nami first: $ nami install nami")
}
return nil
}
app.Commands = []*cli.Command{
&cli.Command{
Name: "install",
Usage: "Install package. $ nami install nami",
Action: func(c *cli.Context) error {
n, err := NewNami()
if err != nil {
return err
}
defer n.Close()
if c.Args().Len() == 0 {
cli.ShowCommandHelp(c, "install")
return nil
}
for _, v := range c.Args().Slice() {
name, script, js, err := n.Parse(v)
if err != nil {
return err
}
f, err := n.Install(name, script, js)
if err != nil {
return err
}
n.Print(name, false)
if f != nil {
defer f()
}
}
return nil
},
},
&cli.Command{
Name: "remove",
Usage: "Remove package. $ nami remove brook",
Action: func(c *cli.Context) error {
n, err := NewNami()
if err != nil {
return err
}
defer n.Close()
if c.Args().Len() == 0 {
cli.ShowCommandHelp(c, "remove")
return nil
}
for _, v := range c.Args().Slice() {
if err := n.Remove(v); err != nil {
return err
}
}
return nil
},
},
&cli.Command{
Name: "list",
Usage: "Print installed packages. $ nami list",
Action: func(c *cli.Context) error {
n, err := NewNami()
if err != nil {
return err
}
defer n.Close()
n.PrintAll()
return nil
},
},
&cli.Command{
Name: "config",
Usage: "Configure key and value. $ nami config <key> <value>. See all keys, $ nami config",
Action: func(c *cli.Context) error {
n, err := NewNami()
if err != nil {
return err
}
defer n.Close()
if c.Args().Len() == 0 {
n.PrintConfigs()
return nil
}
if c.Args().Len() != 2 {
cli.ShowCommandHelp(c, "config")
return nil
}
return n.SetConfig(c.Args().Slice()[0], c.Args().Slice()[1])
},
},
&cli.Command{
Name: "release",
Usage: "Create or update a version with binaries directory on your github project, such as $ nami release github.com/txthinking/nami v1.1.1 ./binaries/",
Action: func(c *cli.Context) error {
n, err := NewNami()
if err != nil {
return err
}
defer n.Close()
if c.Args().Len() != 3 {
cli.ShowCommandHelp(c, "release")
return nil
}
p := &Github{}
err = p.Init(n)
if err != nil {
return err
}
return p.Release(c.Args().Slice()[0], c.Args().Slice()[1], c.Args().Slice()[2])
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}