Skip to content

Commit 5d2e103

Browse files
author
Tony Worm
committed
hof/mod: proxy all commands to upstream cue
1 parent ca9f7ce commit 5d2e103

File tree

12 files changed

+53
-905
lines changed

12 files changed

+53
-905
lines changed

cmd/hof/cmd/mod.go

+46-160
Original file line numberDiff line numberDiff line change
@@ -1,198 +1,84 @@
11
package cmd
22

33
import (
4+
"bytes"
5+
"context"
46
"fmt"
57
"os"
6-
"strconv"
8+
"strings"
79

810
"github.com/spf13/cobra"
9-
10-
"github.com/hofstadter-io/hof/cmd/hof/cmd/mod"
11+
cuecmd "cuelang.org/go/cmd/cue/cmd"
1112

1213
"github.com/hofstadter-io/hof/cmd/hof/ga"
1314
)
1415

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)
8618

87-
# Update dependencies
88-
hof mod get github.com/hofstadter-io/hof@latest
89-
hof mod get all@latest
19+
var buf bytes.Buffer
9020

91-
# Symlink dependencies from local cache
92-
hof mod link
21+
c.SetOutput(&buf)
9322

94-
# Copy dependency code from local cache
95-
hof mod vendor
23+
err := c.Run(context.Background())
9624

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)
9928

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)
11929
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)
12532
}
126-
127-
return nil
12833
}
12934

13035
var ModCmd = &cobra.Command{
13136

13237
Use: "mod",
13338

134-
Aliases: []string{
135-
"m",
136-
},
137-
13839
Short: "CUE module dependency management",
13940

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",
14642

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...))
15245
},
15346
}
15447

155-
func init() {
156-
extra := func(cmd *cobra.Command) bool {
157-
158-
return false
159-
}
160-
161-
ohelp := ModCmd.HelpFunc()
162-
ousage := ModCmd.UsageFunc()
16348

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+
}
16559

60+
func init() {
61+
ModCmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
16662
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+
},
17675
}
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+
})
17980

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)
18582
}
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)
19783

19884
}

cmd/hof/cmd/mod/clean.go

-87
This file was deleted.

0 commit comments

Comments
 (0)