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

Commit

Permalink
add project validation to project client
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Silverio <joshua@acorn.io>
  • Loading branch information
jsilverio22 committed Mar 16, 2023
1 parent 001834e commit 2c41c15
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 22 deletions.
4 changes: 4 additions & 0 deletions integration/projectconfig/projectconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ func TestCLIConfig(t *testing.T) {
wantRestConfigHost: "https://foo.example.com",
wantNamespace: "bar",
wantToken: "pass",
wantErr: fmt.Errorf("project \"example.com/foo/bar\" does not exist within the current context"),
wantError: true,
},
{
name: "Project arg overrides current project",
Expand Down Expand Up @@ -164,6 +166,8 @@ func TestCLIConfig(t *testing.T) {
wantProject: "example.com/foo/bar",
wantNamespace: "bar",
wantToken: "pass",
wantError: true,
wantErr: fmt.Errorf("project \"example.com/foo/bar\" does not exist within the current context"),
},
{
name: "Use alias",
Expand Down
18 changes: 0 additions & 18 deletions pkg/cli/acorn.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (

cli "github.com/acorn-io/acorn/pkg/cli/builder"
"github.com/acorn-io/acorn/pkg/client/term"
"github.com/acorn-io/acorn/pkg/config"
"github.com/acorn-io/acorn/pkg/project"
"github.com/google/go-containerregistry/pkg/logs"
"github.com/pterm/pterm"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -122,22 +120,6 @@ func (a *Acorn) PersistentPre(cmd *cobra.Command, args []string) error {
}
}

if a.Project != "" {
clientFactory := CommandClientFactory{
cmd: cmd,
acorn: a,
}
cfg, err := config.ReadCLIConfig()
if err != nil {
return err
}

err = project.Exists(cmd.Context(), clientFactory.Options().WithCLIConfig(cfg), a.Project)
if err != nil {
return err
}
}

return nil
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/cli/acorn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestAcornProject(t *testing.T) {
client: &testdata.MockClient{},
},
wantErr: true,
wantOut: "projects.api.acorn.io \"noproject\" not found",
wantOut: "project \"noproject\" does not exist within the current context",
},
{
name: "acorn image -j 12.badname",
Expand All @@ -131,7 +131,7 @@ func TestAcornProject(t *testing.T) {
client: &testdata.MockClient{},
},
wantErr: true,
wantOut: "invalid project name [12.badname]: can not contain \".\"",
wantOut: "project \"12.badname\" does not exist within the current context",
},
{
name: "acorn image -j badname/projectname/here",
Expand All @@ -151,7 +151,7 @@ func TestAcornProject(t *testing.T) {
client: &testdata.MockClient{},
},
wantErr: true,
wantOut: "failed to find authentication token for server badname, please run 'acorn login badname' first",
wantOut: "project \"badname/projectname/here\" does not exist within the current context",
},
}
for _, tt := range tests {
Expand Down
6 changes: 5 additions & 1 deletion pkg/project/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/acorn-io/acorn/pkg/hub"
"github.com/acorn-io/acorn/pkg/system"
"github.com/acorn-io/baaah/pkg/restconfig"
"golang.org/x/exp/slices"
"k8s.io/client-go/rest"
)

Expand Down Expand Up @@ -202,8 +203,8 @@ func getClient(ctx context.Context, cfg *config.CLIConfig, opts Options, project
}

func getDesiredProjects(ctx context.Context, cfg *config.CLIConfig, opts Options) (result []string, err error) {
projects, _, err := List(ctx, opts.WithCLIConfig(cfg))
if opts.AllProjects {
projects, _, err := List(ctx, opts.WithCLIConfig(cfg))
return projects, err
}
p := strings.TrimSpace(opts.Project)
Expand All @@ -213,6 +214,9 @@ func getDesiredProjects(ctx context.Context, cfg *config.CLIConfig, opts Options
if strings.TrimSpace(p) == "" {
return nil, nil
}
if len(projects) != 0 && !slices.Contains(projects, p) {
return nil, fmt.Errorf("project \"%s\" does not exist within the current context", p)
}
for _, project := range csvSplit.Split(p, -1) {
if target := cfg.ProjectAliases[project]; target == "" {
result = append(result, project)
Expand Down

0 comments on commit 2c41c15

Please sign in to comment.