gitflow: multi env support

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi
2021-05-18 19:15:17 -07:00
parent f0156f449f
commit 90abadf0de
14 changed files with 262 additions and 211 deletions

View File

@@ -9,13 +9,37 @@ import (
"github.com/spf13/viper"
)
func GetCurrentEnvironmentState(ctx context.Context) *state.State {
func CurrentWorkspace(ctx context.Context) *state.Workspace {
lg := log.Ctx(ctx)
// If no environment name has been given, look for the current environment
environment := viper.GetString("environment")
if environment == "" {
st, err := state.Current(ctx)
if workspacePath := viper.GetString("workspace"); workspacePath != "" {
workspace, err := state.Open(ctx, workspacePath)
if err != nil {
lg.
Fatal().
Err(err).
Str("path", workspacePath).
Msg("failed to open workspace")
}
return workspace
}
workspace, err := state.Current(ctx)
if err != nil {
lg.
Fatal().
Err(err).
Msg("failed to determine current workspace")
}
return workspace
}
func CurrentEnvironmentState(ctx context.Context, workspace *state.Workspace) *state.State {
lg := log.Ctx(ctx)
environmentName := viper.GetString("environment")
if environmentName != "" {
st, err := workspace.Get(ctx, environmentName)
if err != nil {
lg.
Fatal().
@@ -25,38 +49,33 @@ func GetCurrentEnvironmentState(ctx context.Context) *state.State {
return st
}
// At this point, it must be an environment name
workspace := viper.GetString("workspace")
var err error
if workspace == "" {
workspace, err = state.CurrentWorkspace(ctx)
if err != nil {
lg.
Fatal().
Err(err).
Msg("failed to determine current workspace")
}
}
environments, err := state.List(ctx, workspace)
environments, err := workspace.List(ctx)
if err != nil {
lg.
Fatal().
Err(err).
Msg("failed to list environments")
}
for _, e := range environments {
if e.Name == environment {
return e
}
if len(environments) == 0 {
lg.
Fatal().
Msg("no environments")
}
lg.
Fatal().
Str("environment", environment).
Msg("environment not found")
if len(environments) > 1 {
envNames := []string{}
for _, e := range environments {
envNames = append(envNames, e.Name)
}
lg.
Fatal().
Err(err).
Strs("environments", envNames).
Msg("multiple environments available in the workspace, select one with `--environment`")
}
return nil
return environments[0]
}
// Re-compute an environment (equivalent to `dagger up`).

View File

@@ -2,7 +2,6 @@ package cmd
import (
"os"
"path/filepath"
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger/state"
@@ -24,7 +23,7 @@ var initCmd = &cobra.Command{
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
dir := viper.GetString("environment")
dir := viper.GetString("workspace")
if dir == "" {
cwd, err := os.Getwd()
if err != nil {
@@ -36,29 +35,13 @@ var initCmd = &cobra.Command{
dir = cwd
}
var name string
if len(args) > 0 {
name = args[0]
} else {
name = getNewEnvironmentName(dir)
}
_, err := state.Init(ctx, dir, name)
_, err := state.Init(ctx, dir)
if err != nil {
lg.Fatal().Err(err).Msg("failed to initialize")
lg.Fatal().Err(err).Msg("failed to initialize workspace")
}
},
}
func getNewEnvironmentName(dir string) string {
dirName := filepath.Base(dir)
if dirName == "/" {
return "root"
}
return dirName
}
func init() {
if err := viper.BindPFlags(initCmd.Flags()); err != nil {
panic(err)

View File

@@ -1,6 +1,10 @@
package input
import (
"path/filepath"
"strings"
"dagger.io/go/cmd/dagger/cmd/common"
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger/state"
"github.com/spf13/cobra"
@@ -22,7 +26,24 @@ var dirCmd = &cobra.Command{
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
updateEnvironmentInput(ctx, args[0], state.DirInput(args[1], []string{}))
p, err := filepath.Abs(args[1])
if err != nil {
lg.Fatal().Err(err).Str("path", args[1]).Msg("unable to resolve path")
}
workspace := common.CurrentWorkspace(ctx)
if !strings.HasPrefix(p, workspace.Path) {
lg.Fatal().Err(err).Str("path", args[1]).Msg("dir is outside the workspace")
}
p, err = filepath.Rel(workspace.Path, p)
if err != nil {
lg.Fatal().Err(err).Str("path", args[1]).Msg("unable to resolve path")
}
if !strings.HasPrefix(p, ".") {
p = "./" + p
}
updateEnvironmentInput(ctx, args[0], state.DirInput(p, []string{}))
},
}

View File

@@ -31,7 +31,8 @@ var listCmd = &cobra.Command{
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
environment := common.GetCurrentEnvironmentState(ctx)
workspace := common.CurrentWorkspace(ctx)
environment := common.CurrentEnvironmentState(ctx, workspace)
lg = lg.With().
Str("environment", environment.Name).

View File

@@ -35,10 +35,11 @@ func init() {
func updateEnvironmentInput(ctx context.Context, target string, input state.Input) {
lg := log.Ctx(ctx)
st := common.GetCurrentEnvironmentState(ctx)
workspace := common.CurrentWorkspace(ctx)
st := common.CurrentEnvironmentState(ctx, workspace)
st.SetInput(target, input)
if err := state.Save(ctx, st); err != nil {
if err := workspace.Save(ctx, st); err != nil {
lg.Fatal().Err(err).Str("environment", st.Name).Msg("cannot update environment")
}
}

View File

@@ -3,7 +3,6 @@ package input
import (
"dagger.io/go/cmd/dagger/cmd/common"
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger/state"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
@@ -23,10 +22,11 @@ var unsetCmd = &cobra.Command{
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
st := common.GetCurrentEnvironmentState(ctx)
workspace := common.CurrentWorkspace(ctx)
st := common.CurrentEnvironmentState(ctx, workspace)
st.RemoveInputs(args[0])
if err := state.Save(ctx, st); err != nil {
if err := workspace.Save(ctx, st); err != nil {
lg.Fatal().Err(err).Str("environment", st.Name).Msg("cannot update environment")
}
lg.Info().Str("environment", st.Name).Msg("updated environment")

View File

@@ -1,8 +1,6 @@
package cmd
import (
"context"
"errors"
"fmt"
"os"
"os/user"
@@ -10,9 +8,8 @@ import (
"strings"
"text/tabwriter"
"dagger.io/go/cmd/dagger/cmd/common"
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger/state"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
@@ -32,21 +29,8 @@ var listCmd = &cobra.Command{
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
var (
workspace = viper.GetString("workspace")
err error
)
if workspace == "" {
workspace, err = state.CurrentWorkspace(ctx)
if err != nil {
lg.
Fatal().
Err(err).
Msg("failed to determine current workspace")
}
}
environments, err := state.List(ctx, workspace)
workspace := common.CurrentWorkspace(ctx)
environments, err := workspace.List(ctx)
if err != nil {
lg.
Fatal().
@@ -54,34 +38,15 @@ var listCmd = &cobra.Command{
Msg("cannot list environments")
}
environmentPath := getCurrentEnvironmentPath(ctx)
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent)
defer w.Flush()
for _, e := range environments {
line := fmt.Sprintf("%s\t%s\t", e.Name, formatPath(e.Path))
if e.Path == environmentPath {
line = fmt.Sprintf("%s- active environment", line)
}
fmt.Fprintln(w, line)
}
},
}
func getCurrentEnvironmentPath(ctx context.Context) string {
lg := log.Ctx(ctx)
st, err := state.Current(ctx)
if err != nil {
// Ignore error if not initialized
if errors.Is(err, state.ErrNotInit) {
return ""
}
lg.Fatal().Err(err).Msg("failed to load current environment")
}
return st.Path
}
func formatPath(p string) string {
usr, err := user.Current()
if err != nil {

42
cmd/dagger/cmd/new.go Normal file
View File

@@ -0,0 +1,42 @@
package cmd
import (
"dagger.io/go/cmd/dagger/cmd/common"
"dagger.io/go/cmd/dagger/logger"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var newCmd = &cobra.Command{
Use: "new",
Args: cobra.ExactArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
// Fix Viper bug for duplicate flags:
// https://github.com/spf13/viper/issues/233
if err := viper.BindPFlags(cmd.Flags()); err != nil {
panic(err)
}
},
Run: func(cmd *cobra.Command, args []string) {
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
workspace := common.CurrentWorkspace(ctx)
if viper.GetString("environment") != "" {
lg.
Fatal().
Msg("cannot use option -e,--environment for this command")
}
name := args[0]
if _, err := workspace.Create(ctx, name); err != nil {
lg.Fatal().Err(err).Msg("failed to create environment")
}
},
}
func init() {
if err := viper.BindPFlags(newCmd.Flags()); err != nil {
panic(err)
}
}

View File

@@ -30,7 +30,8 @@ var queryCmd = &cobra.Command{
cueOpts := parseQueryFlags()
state := common.GetCurrentEnvironmentState(ctx)
workspace := common.CurrentWorkspace(ctx)
state := common.CurrentEnvironmentState(ctx, workspace)
lg = lg.With().
Str("environment", state.Name).

View File

@@ -34,6 +34,7 @@ func init() {
rootCmd.AddCommand(
initCmd,
newCmd,
computeCmd,
listCmd,
queryCmd,

View File

@@ -3,7 +3,6 @@ package cmd
import (
"dagger.io/go/cmd/dagger/cmd/common"
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger/state"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@@ -24,11 +23,12 @@ var upCmd = &cobra.Command{
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
st := common.GetCurrentEnvironmentState(ctx)
workspace := common.CurrentWorkspace(ctx)
st := common.CurrentEnvironmentState(ctx, workspace)
result := common.EnvironmentUp(ctx, st, viper.GetBool("no-cache"))
st.Computed = result.Computed().JSON().PrettyString()
if err := state.Save(ctx, st); err != nil {
if err := workspace.Save(ctx, st); err != nil {
lg.Fatal().Err(err).Msg("failed to update environment")
}
},