Merge pull request #1695 from talentedmrjones/dagger-project-update

implements dagger project update
This commit is contained in:
Andrea Luzzardi 2022-03-08 16:07:46 -08:00 committed by GitHub
commit ac2e520b37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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{
Args: []string{planPath},
With: viper.GetStringSlice("with"),
Vendor: !viper.GetBool("no-vendor"),
Args: []string{planPath},
With: viper.GetStringSlice("with"),
})
}
@ -106,23 +105,32 @@ func doHelp(cmd *cobra.Command, _ []string) {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.StripEscape)
defer w.Flush()
planPath := viper.GetString("plan")
var (
errorMsg string
loadedMsg string
actionLookupPathMsg string
actions []*plan.Action
)
p, err := loadPlan()
if err != nil {
fmt.Printf("%s", err)
fmt.Fprintln(w, "failed to load plan")
return
errorMsg = "Error: failed to load plan\n\n"
} else {
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")
actionLookupPath := getTargetPath(cmd.Flags().Args())
actions := p.Action().FindByPath(actionLookupPath).Children
fmt.Printf(`Execute a dagger action.
fmt.Printf(`%s%s
%s
Plan loaded from %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, "\t\t")
@ -136,8 +144,7 @@ Plan loaded from %s:
func init() {
doCmd.Flags().StringArrayP("with", "w", []string{}, "")
doCmd.Flags().Bool("no-vendor", false, "Force up, disable inputs check")
doCmd.PersistentFlags().StringP("plan", "p", ".", "Path to plan (defaults to current directory)")
doCmd.Flags().StringP("plan", "p", ".", "Path to plan (defaults to current directory)")
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{
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),
PreRun: func(cmd *cobra.Command, args []string) {
// Fix Viper bug for duplicate flags:

View File

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

View File

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

View File

@ -7,7 +7,6 @@ import (
"github.com/moby/buildkit/util/appcontext"
"github.com/spf13/cobra"
"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/logger"
@ -42,7 +41,6 @@ func init() {
upCmd,
versionCmd,
docCmd,
mod.Cmd,
doCmd,
project.Cmd,
)

View File

@ -82,7 +82,6 @@ func europaUp(ctx context.Context, cl *client.Client, args ...string) error {
Args: args,
With: viper.GetStringSlice("with"),
Target: viper.GetString("target"),
Vendor: !viper.GetBool("no-vendor"),
})
if err != nil {
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().StringArrayP("with", "w", []string{}, "")
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 {
panic(err)

View File

@ -37,7 +37,7 @@ var (
func Vendor(ctx context.Context, p string) error {
if p == "" {
p = GetCueModParent()
p, _ = GetCueModParent()
}
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
func GetCueModParent() string {
func GetCueModParent() (string, bool) {
cwd, _ := os.Getwd()
parentDir := cwd
found := false
for {
if _, err := os.Stat(path.Join(parentDir, "cue.mod")); !errors.Is(err, os.ErrNotExist) {
found = true
break // found it!
}
@ -162,7 +164,7 @@ func GetCueModParent() string {
}
}
return parentDir
return parentDir, found
}
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
With []string
Target string
Vendor bool
}
func Load(ctx context.Context, cfg Config) (*Plan, error) {
log.Ctx(ctx).Debug().Interface("args", cfg.Args).Msg("loading plan")
// FIXME: move vendoring to explicit project update command
if cfg.Vendor {
// FIXME: vendoring path
if err := pkg.Vendor(ctx, ""); err != nil {
return nil, err
}
_, cueModExists := pkg.GetCueModParent()
if !cueModExists {
return nil, fmt.Errorf("dagger project not found. Run `dagger project init`")
}
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
}
@test "project init" {
cd "$TESTDIR"
# mkdir -p ./project/init
"$DAGGER" project init ./project/init --name "github.com/foo/bar"
test -d ./project/init/cue.mod/pkg
test -d ./project/init/cue.mod/usr
test -f ./project/init/cue.mod/module.cue
contents=$(cat ./project/init/cue.mod/module.cue)
@test "project init and update" {
TEMPDIR=$(mktemp -d)
echo "TEMPDIR=$TEMPDIR"
cd "$TEMPDIR"
"$DAGGER" project init ./ --name "github.com/foo/bar"
test -d ./cue.mod/pkg
test -d ./cue.mod/usr
test -f ./cue.mod/module.cue
contents=$(cat ./cue.mod/module.cue)
[ "$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