changed workspace to project
Signed-off-by: Richard Jones <richard@dagger.io>
This commit is contained in:
@@ -13,37 +13,37 @@ import (
|
||||
"go.dagger.io/dagger/state"
|
||||
)
|
||||
|
||||
func CurrentWorkspace(ctx context.Context) *state.Workspace {
|
||||
func CurrentProject(ctx context.Context) *state.Project {
|
||||
lg := log.Ctx(ctx)
|
||||
|
||||
if workspacePath := viper.GetString("workspace"); workspacePath != "" {
|
||||
workspace, err := state.Open(ctx, workspacePath)
|
||||
if projectPath := viper.GetString("project"); projectPath != "" {
|
||||
project, err := state.Open(ctx, projectPath)
|
||||
if err != nil {
|
||||
lg.
|
||||
Fatal().
|
||||
Err(err).
|
||||
Str("path", workspacePath).
|
||||
Msg("failed to open workspace")
|
||||
Str("path", projectPath).
|
||||
Msg("failed to open project")
|
||||
}
|
||||
return workspace
|
||||
return project
|
||||
}
|
||||
|
||||
workspace, err := state.Current(ctx)
|
||||
project, err := state.Current(ctx)
|
||||
if err != nil {
|
||||
lg.
|
||||
Fatal().
|
||||
Err(err).
|
||||
Msg("failed to determine current workspace")
|
||||
Msg("failed to determine current project")
|
||||
}
|
||||
return workspace
|
||||
return project
|
||||
}
|
||||
|
||||
func CurrentEnvironmentState(ctx context.Context, workspace *state.Workspace) *state.State {
|
||||
func CurrentEnvironmentState(ctx context.Context, project *state.Project) *state.State {
|
||||
lg := log.Ctx(ctx)
|
||||
|
||||
environmentName := viper.GetString("environment")
|
||||
if environmentName != "" {
|
||||
st, err := workspace.Get(ctx, environmentName)
|
||||
st, err := project.Get(ctx, environmentName)
|
||||
if err != nil {
|
||||
lg.
|
||||
Fatal().
|
||||
@@ -53,7 +53,7 @@ func CurrentEnvironmentState(ctx context.Context, workspace *state.Workspace) *s
|
||||
return st
|
||||
}
|
||||
|
||||
environments, err := workspace.List(ctx)
|
||||
environments, err := project.List(ctx)
|
||||
if err != nil {
|
||||
lg.
|
||||
Fatal().
|
||||
@@ -76,7 +76,7 @@ func CurrentEnvironmentState(ctx context.Context, workspace *state.Workspace) *s
|
||||
Fatal().
|
||||
Err(err).
|
||||
Strs("environments", envNames).
|
||||
Msg("multiple environments available in the workspace, select one with `--environment`")
|
||||
Msg("multiple environments available in the project, select one with `--environment`")
|
||||
}
|
||||
|
||||
return environments[0]
|
||||
|
@@ -32,9 +32,9 @@ func commandName(cmd *cobra.Command) string {
|
||||
return strings.Join(parts, " ")
|
||||
}
|
||||
|
||||
// TrackWorkspaceCommand is like TrackCommand but includes workspace and
|
||||
// TrackProjectCommand is like TrackCommand but includes project and
|
||||
// optionally environment metadata.
|
||||
func TrackWorkspaceCommand(ctx context.Context, cmd *cobra.Command, w *state.Workspace, env *state.State, props ...*telemetry.Property) chan struct{} {
|
||||
func TrackProjectCommand(ctx context.Context, cmd *cobra.Command, w *state.Project, env *state.State, props ...*telemetry.Property) chan struct{} {
|
||||
props = append([]*telemetry.Property{
|
||||
{
|
||||
// Hash the repository URL for privacy
|
||||
@@ -42,8 +42,8 @@ func TrackWorkspaceCommand(ctx context.Context, cmd *cobra.Command, w *state.Wor
|
||||
Value: hash(gitRepoURL(w.Path)),
|
||||
},
|
||||
{
|
||||
// The workspace path might contain the username (e.g. /home/user/workspace), so we hash itfor privacy.
|
||||
Name: "workspace_path_hash",
|
||||
// The project path might contain the username (e.g. /home/user/project), so we hash itfor privacy.
|
||||
Name: "project_path_hash",
|
||||
Value: hash(w.Path),
|
||||
},
|
||||
}, props...)
|
||||
|
@@ -34,14 +34,14 @@ var editCmd = &cobra.Command{
|
||||
lg := logger.New()
|
||||
ctx := lg.WithContext(cmd.Context())
|
||||
|
||||
workspace := common.CurrentWorkspace(ctx)
|
||||
st := common.CurrentEnvironmentState(ctx, workspace)
|
||||
project := common.CurrentProject(ctx)
|
||||
st := common.CurrentEnvironmentState(ctx, project)
|
||||
|
||||
lg = lg.With().
|
||||
Str("environment", st.Name).
|
||||
Logger()
|
||||
|
||||
doneCh := common.TrackWorkspaceCommand(ctx, cmd, workspace, st)
|
||||
doneCh := common.TrackProjectCommand(ctx, cmd, project, st)
|
||||
|
||||
data, err := yaml.Marshal(st)
|
||||
if err != nil {
|
||||
@@ -92,7 +92,7 @@ var editCmd = &cobra.Command{
|
||||
lg.Fatal().Err(err).Str("environment", st.Name).Msg("invalid input")
|
||||
}
|
||||
|
||||
if err := workspace.Save(ctx, st); err != nil {
|
||||
if err := project.Save(ctx, st); err != nil {
|
||||
lg.Fatal().Err(err).Msg("failed to save state")
|
||||
}
|
||||
},
|
||||
|
@@ -12,7 +12,7 @@ import (
|
||||
|
||||
var initCmd = &cobra.Command{
|
||||
Use: "init",
|
||||
Short: "Initialize a new empty workspace",
|
||||
Short: "Initialize a new empty project",
|
||||
Args: cobra.NoArgs,
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
// Fix Viper bug for duplicate flags:
|
||||
@@ -25,7 +25,7 @@ var initCmd = &cobra.Command{
|
||||
lg := logger.New()
|
||||
ctx := lg.WithContext(cmd.Context())
|
||||
|
||||
dir := viper.GetString("workspace")
|
||||
dir := viper.GetString("project")
|
||||
if dir == "" {
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
@@ -37,12 +37,12 @@ var initCmd = &cobra.Command{
|
||||
dir = cwd
|
||||
}
|
||||
|
||||
workspace, err := state.Init(ctx, dir)
|
||||
project, err := state.Init(ctx, dir)
|
||||
if err != nil {
|
||||
lg.Fatal().Err(err).Msg("failed to initialize workspace")
|
||||
lg.Fatal().Err(err).Msg("failed to initialize project")
|
||||
}
|
||||
|
||||
<-common.TrackWorkspaceCommand(ctx, cmd, workspace, nil)
|
||||
<-common.TrackProjectCommand(ctx, cmd, project, nil)
|
||||
|
||||
},
|
||||
}
|
||||
|
@@ -37,11 +37,11 @@ var dirCmd = &cobra.Command{
|
||||
lg.Fatal().Err(err).Str("path", args[1]).Msg("dir doesn't exists")
|
||||
}
|
||||
|
||||
workspace := common.CurrentWorkspace(ctx)
|
||||
if !strings.HasPrefix(p, workspace.Path) {
|
||||
lg.Fatal().Err(err).Str("path", args[1]).Msg("dir is outside the workspace")
|
||||
project := common.CurrentProject(ctx)
|
||||
if !strings.HasPrefix(p, project.Path) {
|
||||
lg.Fatal().Err(err).Str("path", args[1]).Msg("dir is outside the project")
|
||||
}
|
||||
p, err = filepath.Rel(workspace.Path, p)
|
||||
p, err = filepath.Rel(project.Path, p)
|
||||
if err != nil {
|
||||
lg.Fatal().Err(err).Str("path", args[1]).Msg("unable to resolve path")
|
||||
}
|
||||
|
@@ -32,14 +32,14 @@ var listCmd = &cobra.Command{
|
||||
lg := logger.New()
|
||||
ctx := lg.WithContext(cmd.Context())
|
||||
|
||||
workspace := common.CurrentWorkspace(ctx)
|
||||
st := common.CurrentEnvironmentState(ctx, workspace)
|
||||
project := common.CurrentProject(ctx)
|
||||
st := common.CurrentEnvironmentState(ctx, project)
|
||||
|
||||
lg = lg.With().
|
||||
Str("environment", st.Name).
|
||||
Logger()
|
||||
|
||||
doneCh := common.TrackWorkspaceCommand(ctx, cmd, workspace, st)
|
||||
doneCh := common.TrackProjectCommand(ctx, cmd, project, st)
|
||||
|
||||
c := common.NewClient(ctx)
|
||||
err := c.Do(ctx, st, func(ctx context.Context, env *environment.Environment, s solver.Solver) error {
|
||||
|
@@ -40,14 +40,14 @@ func init() {
|
||||
func updateEnvironmentInput(ctx context.Context, cmd *cobra.Command, target string, input state.Input) {
|
||||
lg := *log.Ctx(ctx)
|
||||
|
||||
workspace := common.CurrentWorkspace(ctx)
|
||||
st := common.CurrentEnvironmentState(ctx, workspace)
|
||||
project := common.CurrentProject(ctx)
|
||||
st := common.CurrentEnvironmentState(ctx, project)
|
||||
|
||||
lg = lg.With().
|
||||
Str("environment", st.Name).
|
||||
Logger()
|
||||
|
||||
doneCh := common.TrackWorkspaceCommand(ctx, cmd, workspace, st, &telemetry.Property{
|
||||
doneCh := common.TrackProjectCommand(ctx, cmd, project, st, &telemetry.Property{
|
||||
Name: "input_target",
|
||||
Value: target,
|
||||
})
|
||||
@@ -71,7 +71,7 @@ func updateEnvironmentInput(ctx context.Context, cmd *cobra.Command, target stri
|
||||
lg.Fatal().Err(err).Msg("invalid input")
|
||||
}
|
||||
|
||||
if err := workspace.Save(ctx, st); err != nil {
|
||||
if err := project.Save(ctx, st); err != nil {
|
||||
lg.Fatal().Err(err).Msg("cannot update environment")
|
||||
}
|
||||
}
|
||||
|
@@ -22,11 +22,11 @@ var unsetCmd = &cobra.Command{
|
||||
lg := logger.New()
|
||||
ctx := lg.WithContext(cmd.Context())
|
||||
|
||||
workspace := common.CurrentWorkspace(ctx)
|
||||
st := common.CurrentEnvironmentState(ctx, workspace)
|
||||
project := common.CurrentProject(ctx)
|
||||
st := common.CurrentEnvironmentState(ctx, project)
|
||||
st.RemoveInputs(args[0])
|
||||
|
||||
if err := workspace.Save(ctx, st); err != nil {
|
||||
if err := project.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")
|
||||
|
@@ -29,10 +29,10 @@ var listCmd = &cobra.Command{
|
||||
lg := logger.New()
|
||||
ctx := lg.WithContext(cmd.Context())
|
||||
|
||||
workspace := common.CurrentWorkspace(ctx)
|
||||
doneCh := common.TrackWorkspaceCommand(ctx, cmd, workspace, nil)
|
||||
project := common.CurrentProject(ctx)
|
||||
doneCh := common.TrackProjectCommand(ctx, cmd, project, nil)
|
||||
|
||||
environments, err := workspace.List(ctx)
|
||||
environments, err := project.List(ctx)
|
||||
if err != nil {
|
||||
lg.
|
||||
Fatal().
|
||||
|
@@ -24,11 +24,11 @@ const tmpBasePath = "./cue.mod/tmp"
|
||||
type file struct {
|
||||
require []*require
|
||||
|
||||
workspacePath string
|
||||
projectPath string
|
||||
}
|
||||
|
||||
func readPath(workspacePath string) (*file, error) {
|
||||
p := path.Join(workspacePath, filePath)
|
||||
func readPath(projectPath string) (*file, error) {
|
||||
p := path.Join(projectPath, filePath)
|
||||
|
||||
f, err := os.Open(p)
|
||||
if err != nil {
|
||||
@@ -47,7 +47,7 @@ func readPath(workspacePath string) (*file, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
modFile.workspacePath = workspacePath
|
||||
modFile.projectPath = projectPath
|
||||
|
||||
return modFile, nil
|
||||
}
|
||||
@@ -102,7 +102,7 @@ func nonEmptyLines(b []byte) []string {
|
||||
func (f *file) processRequire(req *require, upgrade bool) (bool, error) {
|
||||
var isNew bool
|
||||
|
||||
tmpPath := path.Join(f.workspacePath, tmpBasePath, req.repo)
|
||||
tmpPath := path.Join(f.projectPath, tmpBasePath, req.repo)
|
||||
if err := os.MkdirAll(tmpPath, 0755); err != nil {
|
||||
return false, fmt.Errorf("error creating tmp dir for cloning package")
|
||||
}
|
||||
@@ -117,7 +117,7 @@ func (f *file) processRequire(req *require, upgrade bool) (bool, error) {
|
||||
}
|
||||
|
||||
existing := f.search(req)
|
||||
destPath := path.Join(f.workspacePath, destBasePath)
|
||||
destPath := path.Join(f.projectPath, destBasePath)
|
||||
|
||||
// requirement is new, so we should move the files and add it to the mod file
|
||||
if existing == nil {
|
||||
@@ -167,7 +167,7 @@ func (f *file) processRequire(req *require, upgrade bool) (bool, error) {
|
||||
}
|
||||
|
||||
func (f *file) write() error {
|
||||
return ioutil.WriteFile(path.Join(f.workspacePath, filePath), f.contents().Bytes(), 0600)
|
||||
return ioutil.WriteFile(path.Join(f.projectPath, filePath), f.contents().Bytes(), 0600)
|
||||
}
|
||||
|
||||
func (f *file) contents() *bytes.Buffer {
|
||||
|
@@ -25,15 +25,15 @@ var getCmd = &cobra.Command{
|
||||
lg := logger.New()
|
||||
ctx := lg.WithContext(cmd.Context())
|
||||
|
||||
workspace := common.CurrentWorkspace(ctx)
|
||||
st := common.CurrentEnvironmentState(ctx, workspace)
|
||||
doneCh := common.TrackWorkspaceCommand(ctx, cmd, workspace, st, &telemetry.Property{
|
||||
project := common.CurrentProject(ctx)
|
||||
st := common.CurrentEnvironmentState(ctx, project)
|
||||
doneCh := common.TrackProjectCommand(ctx, cmd, project, st, &telemetry.Property{
|
||||
Name: "packages",
|
||||
Value: args,
|
||||
})
|
||||
|
||||
// read mod file in the current dir
|
||||
modFile, err := readPath(workspace.Path)
|
||||
modFile, err := readPath(project.Path)
|
||||
if err != nil {
|
||||
lg.Fatal().Err(err).Msg("error loading module file")
|
||||
}
|
||||
|
@@ -23,7 +23,7 @@ var newCmd = &cobra.Command{
|
||||
lg := logger.New()
|
||||
ctx := lg.WithContext(cmd.Context())
|
||||
|
||||
workspace := common.CurrentWorkspace(ctx)
|
||||
project := common.CurrentProject(ctx)
|
||||
|
||||
if viper.GetString("environment") != "" {
|
||||
lg.
|
||||
@@ -32,7 +32,7 @@ var newCmd = &cobra.Command{
|
||||
}
|
||||
name := args[0]
|
||||
|
||||
st, err := workspace.Create(ctx, name, state.Plan{
|
||||
st, err := project.Create(ctx, name, state.Plan{
|
||||
Package: viper.GetString("package"),
|
||||
})
|
||||
|
||||
@@ -40,7 +40,7 @@ var newCmd = &cobra.Command{
|
||||
lg.Fatal().Err(err).Msg("failed to create environment")
|
||||
}
|
||||
|
||||
<-common.TrackWorkspaceCommand(ctx, cmd, workspace, st)
|
||||
<-common.TrackProjectCommand(ctx, cmd, project, st)
|
||||
},
|
||||
}
|
||||
|
||||
|
@@ -31,14 +31,14 @@ var listCmd = &cobra.Command{
|
||||
lg := logger.New()
|
||||
ctx := lg.WithContext(cmd.Context())
|
||||
|
||||
workspace := common.CurrentWorkspace(ctx)
|
||||
st := common.CurrentEnvironmentState(ctx, workspace)
|
||||
project := common.CurrentProject(ctx)
|
||||
st := common.CurrentEnvironmentState(ctx, project)
|
||||
|
||||
lg = lg.With().
|
||||
Str("environment", st.Name).
|
||||
Logger()
|
||||
|
||||
doneCh := common.TrackWorkspaceCommand(ctx, cmd, workspace, st)
|
||||
doneCh := common.TrackProjectCommand(ctx, cmd, project, st)
|
||||
|
||||
cl := common.NewClient(ctx)
|
||||
err := cl.Do(ctx, st, func(ctx context.Context, env *environment.Environment, s solver.Solver) error {
|
||||
|
@@ -27,8 +27,8 @@ var queryCmd = &cobra.Command{
|
||||
lg := logger.New()
|
||||
ctx := lg.WithContext(cmd.Context())
|
||||
|
||||
workspace := common.CurrentWorkspace(ctx)
|
||||
state := common.CurrentEnvironmentState(ctx, workspace)
|
||||
project := common.CurrentProject(ctx)
|
||||
state := common.CurrentEnvironmentState(ctx, project)
|
||||
|
||||
lg = lg.With().
|
||||
Str("environment", state.Name).
|
||||
@@ -40,7 +40,7 @@ var queryCmd = &cobra.Command{
|
||||
cuePath = cue.ParsePath(args[0])
|
||||
}
|
||||
|
||||
doneCh := common.TrackWorkspaceCommand(ctx, cmd, workspace, state)
|
||||
doneCh := common.TrackProjectCommand(ctx, cmd, project, state)
|
||||
|
||||
cueVal := compiler.NewValue()
|
||||
|
||||
|
@@ -34,7 +34,7 @@ func init() {
|
||||
"External cache sources (eg. user/app:cache, type=local,src=path/to/dir)")
|
||||
|
||||
rootCmd.PersistentFlags().StringP("environment", "e", "", "Select an environment")
|
||||
rootCmd.PersistentFlags().StringP("workspace", "w", "", "Specify a workspace (defaults to current git repository)")
|
||||
rootCmd.PersistentFlags().String("project", "", "Specify a project directory (defaults to current)")
|
||||
|
||||
rootCmd.PersistentPreRun = func(cmd *cobra.Command, _ []string) {
|
||||
lg := logger.New()
|
||||
|
@@ -34,14 +34,14 @@ var upCmd = &cobra.Command{
|
||||
lg := logger.New()
|
||||
ctx := lg.WithContext(cmd.Context())
|
||||
|
||||
workspace := common.CurrentWorkspace(ctx)
|
||||
st := common.CurrentEnvironmentState(ctx, workspace)
|
||||
project := common.CurrentProject(ctx)
|
||||
st := common.CurrentEnvironmentState(ctx, project)
|
||||
|
||||
lg = lg.With().
|
||||
Str("environment", st.Name).
|
||||
Logger()
|
||||
|
||||
doneCh := common.TrackWorkspaceCommand(ctx, cmd, workspace, st)
|
||||
doneCh := common.TrackProjectCommand(ctx, cmd, project, st)
|
||||
|
||||
cl := common.NewClient(ctx)
|
||||
|
||||
@@ -56,7 +56,7 @@ var upCmd = &cobra.Command{
|
||||
}
|
||||
|
||||
st.Computed = env.Computed().JSON().PrettyString()
|
||||
if err := workspace.Save(ctx, st); err != nil {
|
||||
if err := project.Save(ctx, st); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user