Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

cli: Project ops part 1 - CLI updates #2413

Merged
merged 24 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f53a2d0
add 'Building...' to mirror deploy/release
krantzinator Sep 7, 2021
232893b
update docs to remove app-specific language
krantzinator Sep 10, 2021
e2ffc4f
update more cmd language around apps/projects
krantzinator Sep 10, 2021
4471650
add project scoped option for init
krantzinator Sep 27, 2021
81e000c
add global -project flag; update config cmds
krantzinator Sep 28, 2021
d1f3a36
use WithMultipleApp on 'waypoint build'
krantzinator Sep 28, 2021
1564b69
add WithMultipleApp to all relevant cli cmds; gen website/mdx
krantzinator Sep 30, 2021
295ca32
add app-name headers; move deploy destroy to DoApp
krantzinator Oct 4, 2021
c8742af
changelog
krantzinator Oct 4, 2021
103fae5
move and add more AppName cli ui outputs
krantzinator Oct 6, 2021
81653a7
move DoApp for destroy deploys; improve err msg
krantzinator Oct 6, 2021
232bec5
set refApp on cmd for each app func
krantzinator Oct 7, 2021
37bb79d
remove inclusion of solo project name in regex
krantzinator Oct 7, 2021
64170cb
give project flag an alias
krantzinator Oct 7, 2021
39bdeed
fmt; add project alias to docs
krantzinator Oct 7, 2021
10d7d2d
addressing some PR feedback
krantzinator Oct 8, 2021
794bab2
exit early with warning msg if no artifacts found
krantzinator Oct 8, 2021
caa90fa
default to -project value; fixed -remote usage
krantzinator Oct 8, 2021
87a0295
rebase + make gen/website-mdx
krantzinator Oct 11, 2021
f42b277
fmt
krantzinator Oct 11, 2021
d2cd019
lint
krantzinator Oct 11, 2021
e90e118
check for targeted app on hostname register
krantzinator Oct 12, 2021
4f3f96d
add global flags to hostname cmds
krantzinator Oct 12, 2021
3769269
re-delete things after failed rebase conflict resolution
krantzinator Oct 12, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2413.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
cli: Update base commands to default to all apps within the project if project has more than one application
```
2 changes: 1 addition & 1 deletion builtin/docker/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ func (p *Platform) pullImage(cli *client.Client, log hclog.Logger, ui terminal.U
return status.Errorf(codes.InvalidArgument, "unable to parse image name: %s", in)
}

in = named.Name()
in = named.Name()
log.Debug("pulling image", "image", in)

out, err := cli.ImagePull(context.Background(), in, ipo)
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/artifact_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ func (c *ArtifactBuildCommand) Run(args []string) int {
if err := c.Init(
WithArgs(args),
WithFlags(c.Flags()),
WithSingleApp(),
WithMultipleApp(),
); err != nil {
return 1
}

err := c.DoApp(c.Ctx, func(ctx context.Context, app *clientpkg.App) error {
app.UI.Output("Building %s...", app.Ref().Application, terminal.WithHeaderStyle())
_, err := app.Build(ctx, &pb.Job_BuildOp{
DisablePush: !c.flagPush,
})
Expand Down
17 changes: 16 additions & 1 deletion internal/cli/artifact_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (c *ArtifactListCommand) Run(args []string) int {
if err := c.Init(
WithArgs(args),
WithFlags(c.Flags()),
WithSingleApp(),
WithMultipleApp(),
); err != nil {
return 1
}
Expand All @@ -44,6 +44,13 @@ func (c *ArtifactListCommand) Run(args []string) int {
client := c.project.Client()

err := c.DoApp(c.Ctx, func(ctx context.Context, app *clientpkg.App) error {
if !c.flagJson {
// UI -- this should happen at the top so that the app name shows clearly
// for any errors we may encounter prior to the actual table output
// but we also don't want to corrupt the json
app.UI.Output("%s", app.Ref().Application, terminal.WithHeaderStyle())
}

var wsRef *pb.Ref_Workspace
if !c.flagWorkspaceAll {
wsRef = c.project.WorkspaceRef()
Expand All @@ -60,6 +67,14 @@ func (c *ArtifactListCommand) Run(args []string) int {
c.project.UI.Output(clierrors.Humanize(err), terminal.WithErrorStyle())
return ErrSentinel
}
if len(resp.Artifacts) == 0 {
c.project.UI.Output(
"No artifacts found for application %q",
app.Ref().Application,
terminal.WithWarningStyle(),
)
return nil
}
sort.Sort(serversort.ArtifactStartDesc(resp.Artifacts))

if c.flagJson {
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/artifact_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (c *ArtifactPushCommand) Run(args []string) int {
if err := c.Init(
WithArgs(args),
WithFlags(c.Flags()),
WithSingleApp(),
WithMultipleApp(),
); err != nil {
return 1
}
Expand All @@ -30,6 +30,7 @@ func (c *ArtifactPushCommand) Run(args []string) int {

err := c.DoApp(c.Ctx, func(ctx context.Context, app *clientpkg.App) error {
// Get the most recent build
app.UI.Output("Pushing artifact for %s...", app.Ref().Application, terminal.WithHeaderStyle())
build, err := client.GetLatestBuild(ctx, &pb.GetLatestBuildRequest{
Application: app.Ref(),
Workspace: c.project.WorkspaceRef(),
Expand Down
84 changes: 78 additions & 6 deletions internal/cli/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ type baseCommand struct {
// flagApp is the app to target.
flagApp string

// flagProject is the project to target.
flagProject string

// flagWorkspace is the workspace to work in.
flagWorkspace string

Expand Down Expand Up @@ -232,7 +235,6 @@ func (c *baseCommand) Init(opts ...Option) error {
match := reAppTarget.FindStringSubmatch(c.args[0])
if match != nil {
// Set our refs
c.refProject = &pb.Ref_Project{Project: match[1]}
c.refApp = &pb.Ref_Application{
Project: match[1],
Application: match[2],
Expand All @@ -254,7 +256,7 @@ func (c *baseCommand) Init(opts ...Option) error {

// Some CLIs don't explicitly need an app, but sometimes need to load a config
// and setup the project client with the proper project refs.
if baseCfg.AppOptional {
if baseCfg.AppOptional || baseCfg.ProjectTargetRequired {
// If we have args, attempt to extract there first.
if len(c.args) > 0 {
match := reAppTarget.FindStringSubmatch(c.args[0])
Expand All @@ -271,7 +273,10 @@ func (c *baseCommand) Init(opts ...Option) error {

// Explicitly set remote
c.flagRemote = true
} else {

// the below should only be used for commands that don't accept
// other arguments
} else if !baseCfg.ProjectTargetRequired {
// Assume the target is just project
p := c.args[0]
c.refProject = &pb.Ref_Project{Project: p}
Expand Down Expand Up @@ -304,7 +309,19 @@ func (c *baseCommand) Init(opts ...Option) error {

c.cfg = cfg
if cfg != nil {
c.refProject = &pb.Ref_Project{Project: cfg.Project}

// If we require a project target and we still haven't set it,
// and the user provided it via the CLI, set it now.
// If they didn't provide a value via flag, we default to
// the project from initConfig.
if baseCfg.ProjectTargetRequired &&
c.refProject == nil {
if c.flagProject != "" {
c.refProject = &pb.Ref_Project{Project: c.flagProject}
} else {
c.refProject = &pb.Ref_Project{Project: cfg.Project}
}
}

// If we require an app target and we still haven't set it,
// and the user provided it via the CLI, set it now. This code
Expand All @@ -314,7 +331,7 @@ func (c *baseCommand) Init(opts ...Option) error {
c.refApp == nil &&
c.flagApp != "" {
c.refApp = &pb.Ref_Application{
Project: cfg.Project,
Project: c.refProject.Project,
Application: c.flagApp,
}
}
Expand Down Expand Up @@ -373,6 +390,19 @@ func (c *baseCommand) Init(opts ...Option) error {
}
}

if baseCfg.ProjectTargetRequired {
if c.refProject == nil {
if len(c.cfg.Project) == 0 {
c.ui.Output(errProjectMode, terminal.WithErrorStyle())
return ErrSentinel
}

c.refProject = &pb.Ref_Project{
Project: c.cfg.Project,
}
}
}

return nil
}

Expand All @@ -389,9 +419,39 @@ func (c *baseCommand) Init(opts ...Option) error {
// will stop any remaining callbacks and exit early.
func (c *baseCommand) DoApp(ctx context.Context, f func(context.Context, *clientpkg.App) error) error {
var appTargets []string

// If the user specified a project flag, we want only the apps
// that are assigned to that project
if c.flagProject != "" {
client := c.project.Client()
projectTarget := &pb.Ref_Project{Project: c.flagProject}
resp, err := client.GetProject(c.Ctx, &pb.GetProjectRequest{
Project: &pb.Ref_Project{
Project: projectTarget.Project,
},
})
if err != nil {
c.ui.Output(clierrors.Humanize(err), terminal.WithErrorStyle())
return ErrSentinel
}
project := resp.Project

for _, a := range project.Applications {
appTargets = append(appTargets, a.Name)
}
}

if c.flagApp != "" {
c.refApp = &pb.Ref_Application{
Application: c.flagApp,
}
}

// if we specifically target an app, we no longer care about the rest
// of the apps in the project that we set above
if c.refApp != nil {
appTargets = []string{c.refApp.Application}
} else if c.cfg != nil {
} else if c.cfg != nil && len(appTargets) == 0 {
appTargets = append(appTargets, c.cfg.Apps()...)
}

Expand Down Expand Up @@ -474,6 +534,14 @@ func (c *baseCommand) flagSet(bit flagSetBit, f func(*flag.Sets)) *flag.Sets {
Target: &c.flagWorkspace,
Usage: "Workspace to operate in.",
})

f.StringVar(&flag.StringVar{
Name: "project",
Target: &c.flagProject,
Aliases: []string{"p"},
Default: "",
Usage: "Project to target.",
})
}

if bit&flagSetOperation != 0 {
Expand Down Expand Up @@ -688,6 +756,10 @@ This command requires a single targeted app. You have multiple apps defined
so you can specify the app to target using the "-app" flag.
`)

errProjectMode = strings.TrimSpace(`
This command requires a targeted project.`)

// matches either "project" or "project/app"
reAppTarget = regexp.MustCompile(`^(?P<project>[-0-9A-Za-z_]+)/(?P<app>[-0-9A-Za-z_]+)$`)

snapshotUnimplementedErr = strings.TrimSpace(`
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/build_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (c *BuildListCommand) Run(args []string) int {
if err := c.Init(
WithArgs(args),
WithFlags(c.Flags()),
WithSingleApp(),
WithMultipleApp(),
); err != nil {
return 1
}
Expand Down Expand Up @@ -95,6 +95,7 @@ func (c *BuildListCommand) Run(args []string) int {
})
}

app.UI.Output("%s", app.Ref().Application, terminal.WithHeaderStyle())
c.ui.Table(table)

return nil
Expand Down
28 changes: 9 additions & 19 deletions internal/cli/config_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ import (
type ConfigGetCommand struct {
*baseCommand

json bool
raw bool
flagProject string
flagRunner bool
flagLabels map[string]string
json bool
raw bool
flagRunner bool
flagLabels map[string]string
}

func (c *ConfigGetCommand) Run(args []string) int {
Expand Down Expand Up @@ -236,15 +235,6 @@ func (c *ConfigGetCommand) Flags() *flag.Sets {
Target: &c.flagLabels,
Usage: "Labels to set for this operation. Can be specified multiple times.",
})

f.StringVar(&flag.StringVar{
Name: "project",
Target: &c.flagProject,
Usage: "The name of the project for 'project' or 'app' scopes specified " +
"with -scope. This is not required if scope is global or there is " +
"a local waypoint.hcl file.",
Default: "",
})
})
}

Expand All @@ -269,15 +259,15 @@ Usage: waypoint config get [prefix]

The output of this command depends on whether an exact application is
specified or not. If no app is specified with "-app", this will list
all POSSIBLE config vars that could be set for an application. It is
the list of "possible" variables because some are dependent on the application
name, workspace, or labels.
all POSSIBLE config vars that could be set for applications within the project.
It is the list of "possible" variables because some are dependent on the
application name, workspace, or labels.

By specifying the "-app" flag you can look at config variables that
would be resolved for a specific application. This will never show duplicate
variables and will show the full resolved list of variables that will be
set for a specific application. This will default to a workspace of "default"
and empty labels.
set for a specific application. This will default to a workspace of "default,"
the current project, and empty labels.

To further simulate what config variables an application would receive,
you may specify the "-workspace" flag or the "-label" flag (repeatedly)
Expand Down
14 changes: 4 additions & 10 deletions internal/cli/config_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ type ConfigSetCommand struct {

flagGlobal bool
flagRunner bool
flagProject string
flagScope string
flagWorkspaceScope string
flagLabelScope string
Expand Down Expand Up @@ -176,15 +175,6 @@ func (c *ConfigSetCommand) Flags() *flag.Sets {
Default: "project",
})

f.StringVar(&flag.StringVar{
Name: "project",
Target: &c.flagProject,
Usage: "The name of the project for 'project' or 'app' scopes specified " +
"with -scope. This is not required if scope is global or there is " +
"a local waypoint.hcl file.",
Default: "",
})

f.StringVar(&flag.StringVar{
Name: "workspace-scope",
Target: &c.flagWorkspaceScope,
Expand Down Expand Up @@ -237,6 +227,10 @@ Usage: waypoint config set <name>=<value>
environment variable.

This will scope the variable to the entire project by default.
Specify the "-project" flag to set a config variable for a specific project.
The "-project" flag is not required if scope is global or there is a local
waypoint.hcl file.

Specify the "-app" flag to set a config variable for a specific app.

` + c.Flags().Help())
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/config_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (c *ConfigSyncCommand) Run(args []string) int {
if err := c.Init(
WithArgs(args),
WithFlags(c.Flags()),
WithSingleApp(),
WithMultipleApp(),
); err != nil {
return 1
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/deployment_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (c *DeploymentCreateCommand) Run(args []string) int {
if err := c.Init(
WithArgs(args),
WithFlags(c.Flags()),
WithSingleApp(),
WithMultipleApp(),
); err != nil {
return 1
}
Expand All @@ -43,7 +43,7 @@ func (c *DeploymentCreateCommand) Run(args []string) int {
}

// Push it
app.UI.Output("Deploying...", terminal.WithHeaderStyle())
app.UI.Output("Deploying %s...", app.Ref().Application, terminal.WithHeaderStyle())
result, err := app.Deploy(ctx, &pb.Job_DeployOp{
Artifact: push,
})
Expand Down
Loading