Skip to content

Commit

Permalink
hof/mod: proxy all commands to upstream cue
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Worm committed Jan 11, 2025
1 parent ca9f7ce commit 5d2e103
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 905 deletions.
206 changes: 46 additions & 160 deletions cmd/hof/cmd/mod.go
Original file line number Diff line number Diff line change
@@ -1,198 +1,84 @@
package cmd

import (
"bytes"
"context"
"fmt"
"os"
"strconv"
"strings"

"github.com/spf13/cobra"

"github.com/hofstadter-io/hof/cmd/hof/cmd/mod"
cuecmd "cuelang.org/go/cmd/cue/cmd"

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

var modLong = `hof mod is CUE dependency management based on Go mods.
### Module File
The module file holds the requirements for project.
It is found in cue.mod/module.cue
---
// These are like golang import paths
// i.e. github.com/hofstadter-io/hof
module: "<module-path>"
cue: "v0.5.0"
// Required dependencies section
require: {
// "<module-path>": "<module-semver>"
"github.com/hofstadter-io/ghacue": "v0.2.0"
"github.com/hofstadter-io/hofmod-cli": "v0.8.1"
}
// Indirect dependencies (managed by hof)
indirect: { ... }
// Replace dependencies with local or remote
replace: {
"github.com/hofstadter-io/ghacue": "github.com/myorg/ghacue": "v0.4.2"
"github.com/hofstadter-io/hofmod-cli": "../mods/clie"
}
---
### Authentication and private modules
hof mod prefers authenticated requests when fetching dependencies.
This increase rate limits with hosts and supports private modules.
Both token and sshkey base methods are supported, with preferences:
1. Matching entry in .netrc
machine github.com
login github-token
password <github-token-value>
2. ENV VARS for well known hosts.
GITHUB_TOKEN
GITLAB_TOKEN
BITBUCKET_USERNAME / BITBUCKET_PASSWORD or BITBUCKET_TOKEN
The bitbucket method will depend on the account type and enterprise license.
3. SSH keys
the following are searched: ~/.ssh/config, /etc/ssh/config, ~/.ssh/id_rsa
### Usage
there are two main commands you will use, init & tidy
# Initialize the current folder as a module
hof mod init <module-path> (like github.com/org/repo)
# Refresh dependencies, discovering any new imports
hof mod tidy
# Add a dependency
hof mod get github.com/hofstadter-io/[email protected]
hof mod get github.com/hofstadter-io/[email protected]
hof mod get github.com/hofstadter-io/hof@latest // latest semver
hof mod get github.com/hofstadter-io/hof@next // next prerelease
hof mod get github.com/hofstadter-io/hof@main // latest commit on branch
func runCueCmd(args []string) {
c, _ := cuecmd.New(args)

# Update dependencies
hof mod get github.com/hofstadter-io/hof@latest
hof mod get all@latest
var buf bytes.Buffer

# Symlink dependencies from local cache
hof mod link
c.SetOutput(&buf)

# Copy dependency code from local cache
hof mod vendor
err := c.Run(context.Background())

# Verify dependency code against cue.mod/sums.cue
hof mod verify
s := buf.String()
s = strings.Replace(s, "cue ", "hof ", -1)
fmt.Println(s)

# This helpful output
hof mod help
`

const modWarning = `
WARNING: hof will be migrating to CUE modules in 0.7.x
We are doing this to work with the broader ecosystem.
Git based repos will need migration to continue working.
Set HOF_DISABLE_MOD_WARNING=true to hide this message.
`

func ModPersistentPreRun(args []string) (err error) {
disable := os.Getenv("HOF_DISABLE_MOD_WARNING")
if disable == "" {
disable = "false"
}
disabled, err := strconv.ParseBool(disable)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}

if !disabled {
fmt.Fprintln(os.Stderr, modWarning)
fmt.Println(err)
os.Exit(1)
}

return nil
}

var ModCmd = &cobra.Command{

Use: "mod",

Aliases: []string{
"m",
},

Short: "CUE module dependency management",

Long: modLong,

PersistentPreRun: func(cmd *cobra.Command, args []string) {
var err error

// Argument Parsing
Long: "CUE module dependency management",

err = ModPersistentPreRun(args)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
Run: func(cmd *cobra.Command, args []string) {
runCueCmd(append([]string{"mod"}, args...))
},
}

func init() {
extra := func(cmd *cobra.Command) bool {

return false
}

ohelp := ModCmd.HelpFunc()
ousage := ModCmd.UsageFunc()

help := func(cmd *cobra.Command, args []string) {
var modsubs = []string{
"edit",
"fix",
"get",
"init",
"publish",
"registry",
"resolve",
"tidy",
}

func init() {
ModCmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
ga.SendCommandPath(cmd.CommandPath() + " help")

if extra(cmd) {
return
}
ohelp(cmd, args)
}
usage := func(cmd *cobra.Command) error {
if extra(cmd) {
return nil
runCueCmd([]string{"mod", "--help"})
})

for _, sub := range modsubs {
cmd := &cobra.Command{
Use: sub,
Run: func(cmd *cobra.Command, args []string) {
runCueCmd(os.Args[1:])
},
FParseErrWhitelist: cobra.FParseErrWhitelist{
UnknownFlags: true,
},
}
return ousage(cmd)
}
cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
ga.SendCommandPath(cmd.CommandPath() + " help")
runCueCmd([]string{"mod", sub, "--help"})
})

thelp := func(cmd *cobra.Command, args []string) {
help(cmd, args)
}
tusage := func(cmd *cobra.Command) error {
return usage(cmd)
ModCmd.AddCommand(cmd)
}
ModCmd.SetHelpFunc(thelp)
ModCmd.SetUsageFunc(tusage)

ModCmd.AddCommand(cmdmod.InitCmd)
ModCmd.AddCommand(cmdmod.GetCmd)
ModCmd.AddCommand(cmdmod.VerifyCmd)
ModCmd.AddCommand(cmdmod.TidyCmd)
ModCmd.AddCommand(cmdmod.LinkCmd)
ModCmd.AddCommand(cmdmod.VendorCmd)
ModCmd.AddCommand(cmdmod.CleanCmd)
ModCmd.AddCommand(cmdmod.PublishCmd)

}
87 changes: 0 additions & 87 deletions cmd/hof/cmd/mod/clean.go

This file was deleted.

Loading

0 comments on commit 5d2e103

Please sign in to comment.