implements dagger project update

Signed-off-by: Richard Jones <richard@dagger.io>
This commit is contained in:
Richard Jones 2022-03-07 15:09:39 -07:00
parent 64e8afb3f3
commit 57cea9eb6c
No known key found for this signature in database
GPG Key ID: CFB3A382EB166F4C
39 changed files with 67 additions and 158 deletions

View File

@ -88,9 +88,8 @@ func loadPlan() (*plan.Plan, error) {
} }
return plan.Load(context.Background(), plan.Config{ return plan.Load(context.Background(), plan.Config{
Args: []string{planPath}, Args: []string{planPath},
With: viper.GetStringSlice("with"), With: viper.GetStringSlice("with"),
Vendor: !viper.GetBool("no-vendor"),
}) })
} }
@ -106,23 +105,32 @@ func doHelp(cmd *cobra.Command, _ []string) {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.StripEscape) w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.StripEscape)
defer w.Flush() defer w.Flush()
planPath := viper.GetString("plan")
var (
errorMsg string
loadedMsg string
actionLookupPathMsg string
actions []*plan.Action
)
p, err := loadPlan() p, err := loadPlan()
if err != nil { if err != nil {
fmt.Printf("%s", err) errorMsg = "Error: failed to load plan\n\n"
fmt.Fprintln(w, "failed to load plan") } else {
return loadedMsg = "Plan loaded from " + planPath
actionLookupPath := getTargetPath(cmd.Flags().Args())
actions = p.Action().FindByPath(actionLookupPath).Children
actionLookupPathMsg = fmt.Sprintf(`%s:`, actionLookupPath.String())
} }
planPath := viper.GetString("plan") fmt.Printf(`%s%s
actionLookupPath := getTargetPath(cmd.Flags().Args())
actions := p.Action().FindByPath(actionLookupPath).Children
fmt.Printf(`Execute a dagger action.
%s %s
Plan loaded from %s:
%s %s
`, cmd.UsageString(), planPath, "\n"+actionLookupPath.String()+":")
%s
`, errorMsg, cmd.Short, cmd.UsageString(), loadedMsg, actionLookupPathMsg)
// fmt.Fprintln(w, "Actions\tDescription\tPackage") // fmt.Fprintln(w, "Actions\tDescription\tPackage")
// fmt.Fprintln(w, "\t\t") // fmt.Fprintln(w, "\t\t")
@ -136,8 +144,7 @@ Plan loaded from %s:
func init() { func init() {
doCmd.Flags().StringArrayP("with", "w", []string{}, "") doCmd.Flags().StringArrayP("with", "w", []string{}, "")
doCmd.Flags().Bool("no-vendor", false, "Force up, disable inputs check") doCmd.Flags().StringP("plan", "p", ".", "Path to plan (defaults to current directory)")
doCmd.PersistentFlags().StringP("plan", "p", ".", "Path to plan (defaults to current directory)")
doCmd.SetHelpFunc(doHelp) doCmd.SetHelpFunc(doHelp)

View File

@ -1,15 +0,0 @@
package mod
import "github.com/spf13/cobra"
// Cmd exposes the top-level command
var Cmd = &cobra.Command{
Use: "mod",
Short: "Manage an environment's dependencies",
}
func init() {
Cmd.AddCommand(
getCmd,
)
}

View File

@ -14,7 +14,7 @@ var sep = string(os.PathSeparator)
var initCmd = &cobra.Command{ var initCmd = &cobra.Command{
Use: fmt.Sprintf("init [path%sto%sproject]", sep, sep), Use: fmt.Sprintf("init [path%sto%sproject]", sep, sep),
Short: "Initialize a new empty project.", Short: "Initialize a new empty project",
Args: cobra.MaximumNArgs(1), Args: cobra.MaximumNArgs(1),
PreRun: func(cmd *cobra.Command, args []string) { PreRun: func(cmd *cobra.Command, args []string) {
// Fix Viper bug for duplicate flags: // Fix Viper bug for duplicate flags:

View File

@ -25,5 +25,6 @@ func init() {
Cmd.AddCommand( Cmd.AddCommand(
initCmd, initCmd,
updateCmd,
) )
} }

View File

@ -1,4 +1,4 @@
package mod package project
import ( import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -8,9 +8,9 @@ import (
"go.dagger.io/dagger/pkg" "go.dagger.io/dagger/pkg"
) )
var getCmd = &cobra.Command{ var updateCmd = &cobra.Command{
Use: "get [packages]", Use: "update [package]",
Short: "download and install dependencies", Short: "Download and install dependencies",
Args: cobra.MaximumNArgs(1), Args: cobra.MaximumNArgs(1),
PreRun: func(cmd *cobra.Command, args []string) { PreRun: func(cmd *cobra.Command, args []string) {
// Fix Viper bug for duplicate flags: // Fix Viper bug for duplicate flags:
@ -26,11 +26,15 @@ var getCmd = &cobra.Command{
var err error var err error
cueModPath := pkg.GetCueModParent() cueModPath, cueModExists := pkg.GetCueModParent()
err = pkg.CueModInit(ctx, cueModPath, "") if !cueModExists {
if err != nil { lg.Fatal().Msg("dagger project not found. Run `dagger project init`")
lg.Fatal().Err(err).Msg("failed to initialize cue.mod") }
panic(err)
if len(args) == 0 {
lg.Debug().Msg("No package specified, updating all packages")
pkg.Vendor(ctx, cueModPath)
return
} }
var update = viper.GetBool("update") var update = viper.GetBool("update")
@ -64,11 +68,11 @@ var getCmd = &cobra.Command{
} }
func init() { func init() {
getCmd.Flags().String("private-key-file", "", "Private ssh key") updateCmd.Flags().String("private-key-file", "", "Private ssh key")
getCmd.Flags().String("private-key-password", "", "Private ssh key password") updateCmd.Flags().String("private-key-password", "", "Private ssh key password")
getCmd.Flags().BoolP("update", "u", false, "Update specified package") updateCmd.Flags().BoolP("update", "u", false, "Update specified package")
if err := viper.BindPFlags(getCmd.Flags()); err != nil { if err := viper.BindPFlags(updateCmd.Flags()); err != nil {
panic(err) panic(err)
} }
} }

View File

@ -7,7 +7,6 @@ import (
"github.com/moby/buildkit/util/appcontext" "github.com/moby/buildkit/util/appcontext"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/cmd/mod"
"go.dagger.io/dagger/cmd/dagger/cmd/project" "go.dagger.io/dagger/cmd/dagger/cmd/project"
"go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/cmd/dagger/logger"
@ -42,7 +41,6 @@ func init() {
upCmd, upCmd,
versionCmd, versionCmd,
docCmd, docCmd,
mod.Cmd,
doCmd, doCmd,
project.Cmd, project.Cmd,
) )

View File

@ -82,7 +82,6 @@ func europaUp(ctx context.Context, cl *client.Client, args ...string) error {
Args: args, Args: args,
With: viper.GetStringSlice("with"), With: viper.GetStringSlice("with"),
Target: viper.GetString("target"), Target: viper.GetString("target"),
Vendor: !viper.GetBool("no-vendor"),
}) })
if err != nil { if err != nil {
lg.Fatal().Err(err).Msg("failed to load plan") lg.Fatal().Err(err).Msg("failed to load plan")
@ -97,7 +96,6 @@ func init() {
upCmd.Flags().BoolP("force", "f", false, "Force up, disable inputs check") upCmd.Flags().BoolP("force", "f", false, "Force up, disable inputs check")
upCmd.Flags().StringArrayP("with", "w", []string{}, "") upCmd.Flags().StringArrayP("with", "w", []string{}, "")
upCmd.Flags().StringP("target", "t", "", "Run a single target of the DAG (for debugging only)") upCmd.Flags().StringP("target", "t", "", "Run a single target of the DAG (for debugging only)")
upCmd.Flags().Bool("no-vendor", false, "Force up, disable inputs check")
if err := viper.BindPFlags(upCmd.Flags()); err != nil { if err := viper.BindPFlags(upCmd.Flags()); err != nil {
panic(err) panic(err)

View File

@ -37,7 +37,7 @@ var (
func Vendor(ctx context.Context, p string) error { func Vendor(ctx context.Context, p string) error {
if p == "" { if p == "" {
p = GetCueModParent() p, _ = GetCueModParent()
} }
cuePkgDir := path.Join(p, "cue.mod", "pkg") cuePkgDir := path.Join(p, "cue.mod", "pkg")
@ -144,12 +144,14 @@ func extractModules(dest string) error {
} }
// GetCueModParent traverses the directory tree up through ancestors looking for a cue.mod folder // GetCueModParent traverses the directory tree up through ancestors looking for a cue.mod folder
func GetCueModParent() string { func GetCueModParent() (string, bool) {
cwd, _ := os.Getwd() cwd, _ := os.Getwd()
parentDir := cwd parentDir := cwd
found := false
for { for {
if _, err := os.Stat(path.Join(parentDir, "cue.mod")); !errors.Is(err, os.ErrNotExist) { if _, err := os.Stat(path.Join(parentDir, "cue.mod")); !errors.Is(err, os.ErrNotExist) {
found = true
break // found it! break // found it!
} }
@ -162,7 +164,7 @@ func GetCueModParent() string {
} }
} }
return parentDir return parentDir, found
} }
func CueModInit(ctx context.Context, parentDir, module string) error { func CueModInit(ctx context.Context, parentDir, module string) error {

View File

@ -1,4 +0,0 @@
# generated by dagger
dagger.lock
dagger.io
universe.dagger.io

View File

@ -0,0 +1 @@
../../../dagger.io

View File

@ -32,18 +32,14 @@ type Config struct {
Args []string Args []string
With []string With []string
Target string Target string
Vendor bool
} }
func Load(ctx context.Context, cfg Config) (*Plan, error) { func Load(ctx context.Context, cfg Config) (*Plan, error) {
log.Ctx(ctx).Debug().Interface("args", cfg.Args).Msg("loading plan") log.Ctx(ctx).Debug().Interface("args", cfg.Args).Msg("loading plan")
// FIXME: move vendoring to explicit project update command _, cueModExists := pkg.GetCueModParent()
if cfg.Vendor { if !cueModExists {
// FIXME: vendoring path return nil, fmt.Errorf("dagger project not found. Run `dagger project init`")
if err := pkg.Vendor(ctx, ""); err != nil {
return nil, err
}
} }
v, err := compiler.Build("", nil, cfg.Args...) v, err := compiler.Build("", nil, cfg.Args...)

View File

@ -1,4 +0,0 @@
# generated by dagger
dagger.lock
dagger.io
universe.dagger.io

1
tests/cue.mod/pkg/dagger.io Symbolic link
View File

@ -0,0 +1 @@
../../../pkg/dagger.io

View File

@ -0,0 +1 @@
../../../pkg/universe.dagger.io

View File

@ -1 +0,0 @@
module: ""

View File

@ -1,4 +0,0 @@
# generated by dagger
dagger.io
universe.dagger.io
dagger.lock

View File

@ -4,13 +4,19 @@ setup() {
common_setup common_setup
} }
@test "project init" { @test "project init and update" {
cd "$TESTDIR" TEMPDIR=$(mktemp -d)
# mkdir -p ./project/init echo "TEMPDIR=$TEMPDIR"
"$DAGGER" project init ./project/init --name "github.com/foo/bar" cd "$TEMPDIR"
test -d ./project/init/cue.mod/pkg
test -d ./project/init/cue.mod/usr "$DAGGER" project init ./ --name "github.com/foo/bar"
test -f ./project/init/cue.mod/module.cue test -d ./cue.mod/pkg
contents=$(cat ./project/init/cue.mod/module.cue) test -d ./cue.mod/usr
test -f ./cue.mod/module.cue
contents=$(cat ./cue.mod/module.cue)
[ "$contents" == 'module: "github.com/foo/bar"' ] [ "$contents" == 'module: "github.com/foo/bar"' ]
}
dagger project update
test -d ./cue.mod/pkg/dagger.io
test -d ./cue.mod/pkg/universe.dagger.io
}

View File

@ -1,4 +0,0 @@
# generated by dagger
dagger.lock
dagger.io
universe.dagger.io

View File

@ -1 +0,0 @@
module: ""

View File

@ -1,4 +0,0 @@
# generated by dagger
dagger.lock
dagger.io
universe.dagger.io

View File

@ -1 +0,0 @@
module: ""

View File

@ -1,4 +0,0 @@
# generated by dagger
dagger.lock
dagger.io
universe.dagger.io

View File

@ -1 +0,0 @@
module: ""

View File

@ -1,4 +0,0 @@
# generated by dagger
dagger.lock
dagger.io
universe.dagger.io

View File

@ -1 +0,0 @@
module: ""

View File

@ -1,3 +0,0 @@
# generated by dagger
dagger.io
dagger.lock

View File

@ -1,25 +0,0 @@
package hidden
import (
)
#Hidden: {
_pull: dagger.#Pull & {
source: "alpine:3.15.0@sha256:e7d88de73db3d3fd9b2d63aa7f447a10fd0220b7cbf39803c803f2af9ba256b3"
}
_write: dagger.#WriteFile & {
input: _pull.output
path: "/testing"
contents: "1,2,3"
permissions: 700
}
_readfile: dagger.#ReadFile & {
input: _write.output
path: "/testing"
} & {
// assert result
contents: "1,2,3"
}
output: _write.output
contents: _readfile.contents
}

View File

@ -1 +0,0 @@
module: ""

View File

@ -1,4 +0,0 @@
# generated by dagger
dagger.lock
dagger.io
universe.dagger.io

View File

@ -1 +0,0 @@
module: ""

View File

@ -1,4 +0,0 @@
# generated by dagger
dagger.lock
dagger.io
universe.dagger.io

View File

@ -1 +0,0 @@
module: ""

View File

@ -1,4 +0,0 @@
# generated by dagger
dagger.lock
dagger.io
universe.dagger.io

View File

@ -1 +0,0 @@
module: ""

View File

@ -1,4 +0,0 @@
# generated by dagger
dagger.lock
dagger.io
universe.dagger.io

View File

@ -1 +0,0 @@
module: ""

View File

@ -1,4 +0,0 @@
# generated by dagger
dagger.lock
dagger.io
universe.dagger.io

View File

@ -1 +0,0 @@
module: ""

View File

@ -1,4 +0,0 @@
# generated by dagger
dagger.lock
dagger.io
universe.dagger.io