|
1 | 1 | package cmd
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
| 5 | + "context" |
4 | 6 | "fmt"
|
5 | 7 | "os"
|
6 |
| - "strconv" |
| 8 | + "strings" |
7 | 9 |
|
8 | 10 | "github.com/spf13/cobra"
|
9 |
| - |
10 |
| - "github.com/hofstadter-io/hof/cmd/hof/cmd/mod" |
| 11 | + cuecmd "cuelang.org/go/cmd/cue/cmd" |
11 | 12 |
|
12 | 13 | "github.com/hofstadter-io/hof/cmd/hof/ga"
|
13 | 14 | )
|
14 | 15 |
|
15 |
| -var modLong = `hof mod is CUE dependency management based on Go mods. |
16 |
| -
|
17 |
| -### Module File |
18 |
| -
|
19 |
| -The module file holds the requirements for project. |
20 |
| -It is found in cue.mod/module.cue |
21 |
| -
|
22 |
| ---- |
23 |
| -// These are like golang import paths |
24 |
| -// i.e. github.com/hofstadter-io/hof |
25 |
| -module: "<module-path>" |
26 |
| -cue: "v0.5.0" |
27 |
| -
|
28 |
| -// Required dependencies section |
29 |
| -require: { |
30 |
| - // "<module-path>": "<module-semver>" |
31 |
| - "github.com/hofstadter-io/ghacue": "v0.2.0" |
32 |
| - "github.com/hofstadter-io/hofmod-cli": "v0.8.1" |
33 |
| -} |
34 |
| -
|
35 |
| -// Indirect dependencies (managed by hof) |
36 |
| -indirect: { ... } |
37 |
| -
|
38 |
| -// Replace dependencies with local or remote |
39 |
| -replace: { |
40 |
| - "github.com/hofstadter-io/ghacue": "github.com/myorg/ghacue": "v0.4.2" |
41 |
| - "github.com/hofstadter-io/hofmod-cli": "../mods/clie" |
42 |
| -} |
43 |
| ---- |
44 |
| -
|
45 |
| -
|
46 |
| -### Authentication and private modules |
47 |
| -
|
48 |
| -hof mod prefers authenticated requests when fetching dependencies. |
49 |
| -This increase rate limits with hosts and supports private modules. |
50 |
| -Both token and sshkey base methods are supported, with preferences: |
51 |
| -
|
52 |
| -1. Matching entry in .netrc |
53 |
| - machine github.com |
54 |
| - login github-token |
55 |
| - password <github-token-value> |
56 |
| -
|
57 |
| -2. ENV VARS for well known hosts. |
58 |
| -
|
59 |
| - GITHUB_TOKEN |
60 |
| - GITLAB_TOKEN |
61 |
| - BITBUCKET_USERNAME / BITBUCKET_PASSWORD or BITBUCKET_TOKEN |
62 |
| -
|
63 |
| - The bitbucket method will depend on the account type and enterprise license. |
64 |
| -
|
65 |
| -3. SSH keys |
66 |
| -
|
67 |
| - the following are searched: ~/.ssh/config, /etc/ssh/config, ~/.ssh/id_rsa |
68 |
| -
|
69 |
| -
|
70 |
| -### Usage |
71 |
| -
|
72 |
| -there are two main commands you will use, init & tidy |
73 |
| -
|
74 |
| -# Initialize the current folder as a module |
75 |
| -hof mod init <module-path> (like github.com/org/repo) |
76 |
| -
|
77 |
| -# Refresh dependencies, discovering any new imports |
78 |
| -hof mod tidy |
79 |
| -
|
80 |
| -# Add a dependency |
81 |
| -hof mod get github.com/hofstadter-io/[email protected] |
82 |
| -hof mod get github.com/hofstadter-io/[email protected] |
83 |
| -hof mod get github.com/hofstadter-io/hof@latest // latest semver |
84 |
| -hof mod get github.com/hofstadter-io/hof@next // next prerelease |
85 |
| -hof mod get github.com/hofstadter-io/hof@main // latest commit on branch |
| 16 | +func runCueCmd(args []string) { |
| 17 | + c, _ := cuecmd.New(args) |
86 | 18 |
|
87 |
| -# Update dependencies |
88 |
| -hof mod get github.com/hofstadter-io/hof@latest |
89 |
| -hof mod get all@latest |
| 19 | + var buf bytes.Buffer |
90 | 20 |
|
91 |
| -# Symlink dependencies from local cache |
92 |
| -hof mod link |
| 21 | + c.SetOutput(&buf) |
93 | 22 |
|
94 |
| -# Copy dependency code from local cache |
95 |
| -hof mod vendor |
| 23 | + err := c.Run(context.Background()) |
96 | 24 |
|
97 |
| -# Verify dependency code against cue.mod/sums.cue |
98 |
| -hof mod verify |
| 25 | + s := buf.String() |
| 26 | + s = strings.Replace(s, "cue ", "hof ", -1) |
| 27 | + fmt.Println(s) |
99 | 28 |
|
100 |
| -# This helpful output |
101 |
| -hof mod help |
102 |
| -
|
103 |
| -` |
104 |
| - |
105 |
| -const modWarning = ` |
106 |
| -WARNING: hof will be migrating to CUE modules in 0.7.x |
107 |
| -We are doing this to work with the broader ecosystem. |
108 |
| -Git based repos will need migration to continue working. |
109 |
| -
|
110 |
| -Set HOF_DISABLE_MOD_WARNING=true to hide this message. |
111 |
| -` |
112 |
| - |
113 |
| -func ModPersistentPreRun(args []string) (err error) { |
114 |
| - disable := os.Getenv("HOF_DISABLE_MOD_WARNING") |
115 |
| - if disable == "" { |
116 |
| - disable = "false" |
117 |
| - } |
118 |
| - disabled, err := strconv.ParseBool(disable) |
119 | 29 | if err != nil {
|
120 |
| - fmt.Fprintln(os.Stderr, err) |
121 |
| - } |
122 |
| - |
123 |
| - if !disabled { |
124 |
| - fmt.Fprintln(os.Stderr, modWarning) |
| 30 | + fmt.Println(err) |
| 31 | + os.Exit(1) |
125 | 32 | }
|
126 |
| - |
127 |
| - return nil |
128 | 33 | }
|
129 | 34 |
|
130 | 35 | var ModCmd = &cobra.Command{
|
131 | 36 |
|
132 | 37 | Use: "mod",
|
133 | 38 |
|
134 |
| - Aliases: []string{ |
135 |
| - "m", |
136 |
| - }, |
137 |
| - |
138 | 39 | Short: "CUE module dependency management",
|
139 | 40 |
|
140 |
| - Long: modLong, |
141 |
| - |
142 |
| - PersistentPreRun: func(cmd *cobra.Command, args []string) { |
143 |
| - var err error |
144 |
| - |
145 |
| - // Argument Parsing |
| 41 | + Long: "CUE module dependency management", |
146 | 42 |
|
147 |
| - err = ModPersistentPreRun(args) |
148 |
| - if err != nil { |
149 |
| - fmt.Println(err) |
150 |
| - os.Exit(1) |
151 |
| - } |
| 43 | + Run: func(cmd *cobra.Command, args []string) { |
| 44 | + runCueCmd(append([]string{"mod"}, args...)) |
152 | 45 | },
|
153 | 46 | }
|
154 | 47 |
|
155 |
| -func init() { |
156 |
| - extra := func(cmd *cobra.Command) bool { |
157 |
| - |
158 |
| - return false |
159 |
| - } |
160 |
| - |
161 |
| - ohelp := ModCmd.HelpFunc() |
162 |
| - ousage := ModCmd.UsageFunc() |
163 | 48 |
|
164 |
| - help := func(cmd *cobra.Command, args []string) { |
| 49 | +var modsubs = []string{ |
| 50 | + "edit", |
| 51 | + "fix", |
| 52 | + "get", |
| 53 | + "init", |
| 54 | + "publish", |
| 55 | + "registry", |
| 56 | + "resolve", |
| 57 | + "tidy", |
| 58 | +} |
165 | 59 |
|
| 60 | +func init() { |
| 61 | + ModCmd.SetHelpFunc(func(cmd *cobra.Command, args []string) { |
166 | 62 | ga.SendCommandPath(cmd.CommandPath() + " help")
|
167 |
| - |
168 |
| - if extra(cmd) { |
169 |
| - return |
170 |
| - } |
171 |
| - ohelp(cmd, args) |
172 |
| - } |
173 |
| - usage := func(cmd *cobra.Command) error { |
174 |
| - if extra(cmd) { |
175 |
| - return nil |
| 63 | + runCueCmd([]string{"mod", "--help"}) |
| 64 | + }) |
| 65 | + |
| 66 | + for _, sub := range modsubs { |
| 67 | + cmd := &cobra.Command{ |
| 68 | + Use: sub, |
| 69 | + Run: func(cmd *cobra.Command, args []string) { |
| 70 | + runCueCmd(os.Args[1:]) |
| 71 | + }, |
| 72 | + FParseErrWhitelist: cobra.FParseErrWhitelist{ |
| 73 | + UnknownFlags: true, |
| 74 | + }, |
176 | 75 | }
|
177 |
| - return ousage(cmd) |
178 |
| - } |
| 76 | + cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) { |
| 77 | + ga.SendCommandPath(cmd.CommandPath() + " help") |
| 78 | + runCueCmd([]string{"mod", sub, "--help"}) |
| 79 | + }) |
179 | 80 |
|
180 |
| - thelp := func(cmd *cobra.Command, args []string) { |
181 |
| - help(cmd, args) |
182 |
| - } |
183 |
| - tusage := func(cmd *cobra.Command) error { |
184 |
| - return usage(cmd) |
| 81 | + ModCmd.AddCommand(cmd) |
185 | 82 | }
|
186 |
| - ModCmd.SetHelpFunc(thelp) |
187 |
| - ModCmd.SetUsageFunc(tusage) |
188 |
| - |
189 |
| - ModCmd.AddCommand(cmdmod.InitCmd) |
190 |
| - ModCmd.AddCommand(cmdmod.GetCmd) |
191 |
| - ModCmd.AddCommand(cmdmod.VerifyCmd) |
192 |
| - ModCmd.AddCommand(cmdmod.TidyCmd) |
193 |
| - ModCmd.AddCommand(cmdmod.LinkCmd) |
194 |
| - ModCmd.AddCommand(cmdmod.VendorCmd) |
195 |
| - ModCmd.AddCommand(cmdmod.CleanCmd) |
196 |
| - ModCmd.AddCommand(cmdmod.PublishCmd) |
197 | 83 |
|
198 | 84 | }
|
0 commit comments