Merge pull request #247 from dagger/cli-new-plan-git

Cli new plan git
This commit is contained in:
Andrea Luzzardi 2021-04-01 18:13:24 -07:00 committed by GitHub
commit 896d303f4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 103 additions and 21 deletions

View File

@ -8,9 +8,9 @@ import (
)
var gitCmd = &cobra.Command{
Use: "git TARGET REMOTE REF [SUBDIR]",
Use: "git TARGET REMOTE [REF] [SUBDIR]",
Short: "Add a git repository as input artifact",
Args: cobra.RangeArgs(3, 4),
Args: cobra.RangeArgs(2, 4),
PreRun: func(cmd *cobra.Command, args []string) {
// Fix Viper bug for duplicate flags:
// https://github.com/spf13/viper/issues/233
@ -22,12 +22,17 @@ var gitCmd = &cobra.Command{
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
ref := "HEAD"
if len(args) > 2 {
ref = args[2]
}
subDir := ""
if len(args) > 3 {
subDir = args[3]
}
updateDeploymentInput(ctx, args[0], dagger.GitInput(args[1], args[2], subDir))
updateDeploymentInput(ctx, args[0], dagger.GitInput(args[1], ref, subDir))
},
}

View File

@ -2,6 +2,7 @@ package cmd
import (
"context"
"net/url"
"os"
"path/filepath"
@ -94,20 +95,48 @@ func getNewDeploymentName(ctx context.Context) string {
return currentDir
}
// FIXME: Implement options: --plan-*
func getPlanSource(ctx context.Context) dagger.Input {
lg := log.Ctx(ctx)
planDir := viper.GetString("plan-dir")
if planDir == "" {
var err error
planDir, err = os.Getwd()
if err != nil {
lg.Fatal().Err(err).Msg("cannot get current working directory")
src := dagger.Input{}
checkFirstSet := func() {
if src.Type != dagger.InputTypeEmpty {
lg.Fatal().Msg("only one of those options can be set: --plan-dir, --plan-git, --plan-package, --plan-file")
}
}
return dagger.DirInput(planDir, []string{"*.cue", "cue.mod"})
planDir := viper.GetString("plan-dir")
planGit := viper.GetString("plan-git")
if planDir != "" {
checkFirstSet()
src = dagger.DirInput(planDir, []string{"*.cue", "cue.mod"})
}
if planGit != "" {
checkFirstSet()
u, err := url.Parse(planGit)
if err != nil {
lg.Fatal().Err(err).Str("url", planGit).Msg("cannot get current working directory")
}
ref := u.Fragment // eg. #main
u.Fragment = ""
remote := u.String()
src = dagger.GitInput(remote, ref, "")
}
if src.Type == dagger.InputTypeEmpty {
var err error
wd, err := os.Getwd()
if err != nil {
lg.Fatal().Err(err).Msg("cannot get current working directory")
}
return dagger.DirInput(wd, []string{"*.cue", "cue.mod"})
}
return src
}
func init() {

View File

@ -1,6 +1,8 @@
package plan
import (
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
@ -17,10 +19,10 @@ var dirCmd = &cobra.Command{
}
},
Run: func(cmd *cobra.Command, args []string) {
// lg := logger.New()
// ctx := lg.WithContext(cmd.Context())
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
panic("not implemented")
updateDeploymentPlan(ctx, dagger.DirInput(args[0], []string{"*.cue", "cue.mod"}))
},
}

View File

@ -1,14 +1,16 @@
package plan
import (
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var gitCmd = &cobra.Command{
Use: "git REMOTE REF [SUBDIR]",
Use: "git REMOTE [REF] [SUBDIR]",
Short: "Load plan from a git package",
Args: cobra.MinimumNArgs(2),
Args: cobra.RangeArgs(1, 3),
PreRun: func(cmd *cobra.Command, args []string) {
// Fix Viper bug for duplicate flags:
// https://github.com/spf13/viper/issues/233
@ -17,10 +19,20 @@ var gitCmd = &cobra.Command{
}
},
Run: func(cmd *cobra.Command, args []string) {
// lg := logger.New()
// ctx := lg.WithContext(cmd.Context())
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
panic("not implemented")
ref := "HEAD"
if len(args) > 1 {
ref = args[1]
}
subDir := ""
if len(args) > 2 {
subDir = args[2]
}
updateDeploymentPlan(ctx, dagger.GitInput(args[0], ref, subDir))
},
}

View File

@ -1,6 +1,13 @@
package plan
import "github.com/spf13/cobra"
import (
"context"
"dagger.io/go/cmd/dagger/cmd/common"
"dagger.io/go/dagger"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
// Cmd exposes the top-level command
var Cmd = &cobra.Command{
@ -16,3 +23,20 @@ func init() {
fileCmd,
)
}
func updateDeploymentPlan(ctx context.Context, planSource dagger.Input) {
lg := log.Ctx(ctx)
store, err := dagger.DefaultStore()
if err != nil {
lg.Fatal().Err(err).Msg("failed to load store")
}
st := common.GetCurrentDeploymentState(ctx, store)
st.PlanSource = planSource
if err := store.UpdateDeployment(ctx, st, nil); err != nil {
lg.Fatal().Err(err).Str("deploymentId", st.ID).Str("deploymentName", st.Name).Msg("cannot update deployment")
}
lg.Info().Str("deploymentId", st.ID).Str("deploymentName", st.Name).Msg("updated deployment")
}

View File

@ -28,6 +28,7 @@ const (
InputTypeText InputType = "text"
InputTypeJSON InputType = "json"
InputTypeYAML InputType = "yaml"
InputTypeEmpty InputType = ""
)
type Input struct {
@ -111,7 +112,16 @@ func GitInput(remote, ref, dir string) Input {
}
func (git gitInput) Compile() (*compiler.Value, error) {
panic("NOT IMPLEMENTED")
ref := "HEAD"
if git.Ref != "" {
ref = git.Ref
}
return compiler.Compile("", fmt.Sprintf(
`#compute: [{do:"fetch-git", remote:"%s", ref:"%s"}]`,
git.Remote,
ref,
))
}
// An input artifact loaded from a docker container