Skip to content

Commit 507f3d8

Browse files
committed
chore: drop more selfhost related code
1 parent 8729f3e commit 507f3d8

File tree

8 files changed

+107
-171
lines changed

8 files changed

+107
-171
lines changed

cmd/axiom/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ func printError(w io.Writer, err error, cmd *cobra.Command) {
112112
// Print some nicer output for Axiom API errors.
113113
if errors.Is(err, axiom.ErrNotFound) || errors.Is(err, axiom.ErrExists) ||
114114
errors.Is(err, axiom.ErrUnauthorized) || errors.Is(err, axiom.ErrUnauthenticated) {
115-
fmt.Fprintf(w, "Error: %s\n", errors.Unwrap(err))
115+
if unwrappedErr := errors.Unwrap(err); unwrappedErr != nil {
116+
err = unwrappedErr
117+
}
118+
fmt.Fprintf(w, "Error: %s\n", err)
116119
return
117120
}
118121

internal/cmd/auth/auth_login.go

Lines changed: 73 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,14 @@ import (
2020
"github.com/axiomhq/cli/pkg/surveyext"
2121
)
2222

23-
const (
24-
oAuth2ClientID = "13c885a8-f46a-4424-82d2-883cf7ccfe49"
25-
26-
typeCloud = "Cloud"
27-
typeSelfhost = "Selfhost"
28-
)
29-
30-
var validDeploymentTypes = []string{typeCloud, typeSelfhost}
23+
const oAuth2ClientID = "13c885a8-f46a-4424-82d2-883cf7ccfe49"
3124

3225
type loginOptions struct {
3326
*cmdutil.Factory
3427

3528
// AutoLogin specifies if the CLI redirects to the Axiom UI for
3629
// authentication.
3730
AutoLogin bool
38-
// Type of the deployment to authenticate with. Default to "Cloud". Can be
39-
// overwritten by flag.
40-
Type string
41-
// Url of the deployment to authenticate with. Default to the Axiom Cloud
42-
// URL. Can be overwritten by flag.
43-
URL string
4431
// Alias of the deployment for future reference. If not supplied as a flag,
4532
// which is optional, the user will be asked for it.
4633
Alias string
@@ -50,10 +37,15 @@ type loginOptions struct {
5037
Token string
5138
// OrganizationID of the organization the supplied token is valid for. If
5239
// not supplied as a flag, which is optional, the user will be asked for it.
53-
// Only valid for cloud deployments.
5440
OrganizationID string
5541
// Force the creation and skip the confirmation prompt.
5642
Force bool
43+
44+
// Alternate deployment support:
45+
46+
// URL of the deployment to authenticate with. Default to the Axiom Cloud
47+
// URL. Can be overwritten by a hidden flag.
48+
URL string
5749
}
5850

5951
// NewLoginCmd creates ans returns the login command.
@@ -63,7 +55,7 @@ func NewLoginCmd(f *cmdutil.Factory) *cobra.Command {
6355
}
6456

6557
cmd := &cobra.Command{
66-
Use: "login [(-t|--type)=cloud|selfhost] [(-u|--url) <url>] [(-a|--alias) <alias>] [(-o|--org-id) <organization-id>] [-f|--force]",
58+
Use: "login [(-a|--alias) <alias>] [(-o|--org-id) <organization-id>] [-f|--force]",
6759
Short: "Login to Axiom",
6860

6961
DisableFlagsInUseLine: true,
@@ -73,21 +65,13 @@ func NewLoginCmd(f *cmdutil.Factory) *cobra.Command {
7365
$ axiom auth login
7466
7567
# Provide parameters on the command-line:
76-
$ echo $AXIOM_ACCESS_TOKEN | axiom auth login --alias="axiom-eu-west-1" --url="https://axiom.eu-west-1.aws.com" -f
68+
$ echo $AXIOM_TOKEN | axiom auth login --alias="axiom-cloud" --org-id="fancy-horse-1234" -f
7769
`),
7870

7971
PreRunE: func(cmd *cobra.Command, _ []string) error {
8072
if !opts.IO.IsStdinTTY() || opts.AutoLogin {
8173
return nil
8274
}
83-
84-
// If the user specifies the url, we assume he wants to authenticate
85-
// against a selfhost deployment unless he explicitly specifies the
86-
// hidden type flag that specifies the type of the deployment.
87-
if cmd.Flag("url").Changed && !cmd.Flag("type").Changed {
88-
opts.Type = typeSelfhost
89-
}
90-
9175
return completeLogin(cmd.Context(), opts)
9276
},
9377

@@ -100,58 +84,30 @@ func NewLoginCmd(f *cmdutil.Factory) *cobra.Command {
10084
}
10185

10286
cmd.Flags().BoolVar(&opts.AutoLogin, "auto-login", true, "Login through the Axiom UI")
103-
cmd.Flags().StringVarP(&opts.Type, "type", "t", strings.ToLower(typeCloud), "Type of the deployment")
104-
cmd.Flags().StringVarP(&opts.URL, "url", "u", client.CloudURL, "Url of the deployment")
10587
cmd.Flags().StringVarP(&opts.Alias, "alias", "a", "", "Alias of the deployment")
10688
cmd.Flags().StringVarP(&opts.OrganizationID, "org-id", "o", "", "Organization ID")
10789
cmd.Flags().BoolVarP(&opts.Force, "force", "f", false, "Skip the confirmation prompt")
90+
cmd.Flags().StringVarP(&opts.URL, "url", "u", client.CloudURL, "Url of the deployment")
10891

10992
_ = cmd.RegisterFlagCompletionFunc("auto-login", cmdutil.NoCompletion)
110-
_ = cmd.RegisterFlagCompletionFunc("type", cmdutil.NoCompletion)
111-
_ = cmd.RegisterFlagCompletionFunc("url", cmdutil.NoCompletion)
11293
_ = cmd.RegisterFlagCompletionFunc("alias", cmdutil.NoCompletion)
11394
_ = cmd.RegisterFlagCompletionFunc("org-id", cmdutil.NoCompletion)
11495
_ = cmd.RegisterFlagCompletionFunc("force", cmdutil.NoCompletion)
96+
_ = cmd.RegisterFlagCompletionFunc("url", cmdutil.NoCompletion)
11597

11698
if !opts.IO.IsStdinTTY() {
11799
_ = cmd.MarkFlagRequired("alias")
100+
_ = cmd.MarkFlagRequired("org-id")
118101
}
119102

120-
_ = cmd.PersistentFlags().MarkHidden("type")
103+
_ = cmd.Flags().MarkHidden("url")
121104

122105
return cmd
123106
}
124107

125108
func completeLogin(ctx context.Context, opts *loginOptions) error {
126-
// 1. Cloud or Selfhost?
127-
if opts.Type == "" {
128-
if err := survey.AskOne(&survey.Select{
129-
Message: "Which kind of Axiom deployment are you using?",
130-
Default: validDeploymentTypes[0],
131-
Options: validDeploymentTypes,
132-
}, &opts.Type, opts.IO.SurveyIO()); err != nil {
133-
return err
134-
}
135-
}
136-
137-
opts.Type = strings.ToLower(opts.Type)
138-
139-
// 2. If Cloud mode but no URL, set the correct URL instead of asking the
140-
// user for it.
141-
if opts.Type == strings.ToLower(typeCloud) && opts.URL == "" {
142-
opts.URL = client.CloudURL
143-
} else if opts.URL == "" {
144-
if err := survey.AskOne(&survey.Input{
145-
Message: "What is the url of the deployment?",
146-
}, &opts.URL, survey.WithValidator(survey.ComposeValidators(
147-
survey.Required,
148-
surveyext.ValidateURL,
149-
)), opts.IO.SurveyIO()); err != nil {
150-
return err
151-
}
152-
}
153-
154-
if opts.URL != "" && !strings.HasPrefix(opts.URL, "http://") && !strings.HasPrefix(opts.URL, "https://") {
109+
// Make sure to accept urls without a scheme.
110+
if !strings.HasPrefix(opts.URL, "http://") && !strings.HasPrefix(opts.URL, "https://") {
155111
opts.URL = "https://" + opts.URL
156112
}
157113

@@ -162,7 +118,7 @@ func completeLogin(ctx context.Context, opts *loginOptions) error {
162118
}
163119
u.Path = "/profile"
164120

165-
// 3. Wheather to open the browser or not.
121+
// 1. Wheather to open the browser or not.
166122
askTokenMsg := "What is your personal access token?"
167123
if ok, err := surveyext.AskConfirm("You need to retrieve a personal access token from your profile page. Should I open that page in your default browser?",
168124
true, opts.IO.SurveyIO()); err != nil {
@@ -173,7 +129,7 @@ func completeLogin(ctx context.Context, opts *loginOptions) error {
173129
return err
174130
}
175131

176-
// 3. The token to use.
132+
// 2. The token to use.
177133
if err := survey.AskOne(&survey.Password{
178134
Message: askTokenMsg,
179135
}, &opts.Token, survey.WithValidator(survey.ComposeValidators(
@@ -183,11 +139,10 @@ func completeLogin(ctx context.Context, opts *loginOptions) error {
183139
return err
184140
}
185141

186-
// 4. Try to authenticate and fetch the organizations available to the user
187-
// in case the deployment is a cloud deployment. If only one organization is
188-
// available, that one is selected by default, without asking the user for
189-
// it.
190-
if opts.Type == strings.ToLower(typeCloud) && opts.OrganizationID == "" {
142+
// 3. Try to authenticate and fetch the organizations available to the user.
143+
// If only one organization is available, that one is selected by default,
144+
// without asking the user for it.
145+
if opts.OrganizationID == "" {
191146
axiomClient, err := client.New(ctx, opts.URL, opts.Token, "axiom", opts.Config.Insecure)
192147
if err != nil {
193148
return err
@@ -238,11 +193,11 @@ func completeLogin(ctx context.Context, opts *loginOptions) error {
238193

239194
// Just use "cloud" as the alias if this is their first deployment and they
240195
// are authenticating against Axiom Cloud.
241-
if hostRef == strings.ToLower(typeCloud) {
196+
if hostRef == "cloud" {
242197
opts.Alias = hostRef
243198
}
244199

245-
// 5. Ask for an alias to use.
200+
// 4. Ask for an alias to use.
246201
if opts.Alias == "" {
247202
if err := survey.AskOne(&survey.Input{
248203
Message: "Under which name should the deployment be referenced in the future?",
@@ -260,28 +215,12 @@ func completeLogin(ctx context.Context, opts *loginOptions) error {
260215
}
261216

262217
func autoLogin(ctx context.Context, opts *loginOptions) error {
263-
opts.Type = strings.ToLower(opts.Type)
264-
265-
// 1. If Cloud mode but no URL, set the correct URL instead of asking the
266-
// user for it.
267-
if opts.Type == strings.ToLower(typeCloud) && opts.URL == "" {
268-
opts.URL = client.CloudURL
269-
} else if opts.URL == "" {
270-
if err := survey.AskOne(&survey.Input{
271-
Message: "What is the url of the deployment?",
272-
}, &opts.URL, survey.WithValidator(survey.ComposeValidators(
273-
survey.Required,
274-
surveyext.ValidateURL,
275-
)), opts.IO.SurveyIO()); err != nil {
276-
return err
277-
}
278-
}
279-
280-
if opts.URL != "" && !strings.HasPrefix(opts.URL, "http://") && !strings.HasPrefix(opts.URL, "https://") {
218+
// Make sure to accept urls without a scheme.
219+
if !strings.HasPrefix(opts.URL, "http://") && !strings.HasPrefix(opts.URL, "https://") {
281220
opts.URL = "https://" + opts.URL
282221
}
283222

284-
// 2. Wheather to open the browser or not. But the URL to open and have the
223+
// 1. Wheather to open the browser or not. But the URL to open and have the
285224
// user login is presented nonetheless.
286225
stop := func() {}
287226
loginFunc := func(_ context.Context, loginURL string) error {
@@ -311,22 +250,49 @@ func autoLogin(ctx context.Context, opts *loginOptions) error {
311250
return err
312251
}
313252

314-
// 3. Try to authenticate and fetch the organizations available to the user
315-
// in case the deployment is a cloud deployment. If at least one
316-
// organization is available, the first one is selected.
317-
if opts.Type == strings.ToLower(typeCloud) && opts.OrganizationID == "" {
253+
// 2. Try to authenticate and fetch the organizations available to the user.
254+
// If only one organization is available, that one is selected by default,
255+
// without asking the user for it.
256+
if opts.OrganizationID == "" {
318257
axiomClient, err := client.New(ctx, opts.URL, opts.Token, "axiom", opts.Config.Insecure)
319258
if err != nil {
320259
return err
321260
}
322261

323-
organizations, err := axiomClient.Organizations.List(ctx)
324-
if err != nil {
262+
if organizations, err := axiomClient.Organizations.List(ctx); err != nil {
325263
return err
326-
}
327-
328-
if len(organizations) > 0 {
264+
} else if len(organizations) == 1 {
329265
opts.OrganizationID = organizations[0].ID
266+
} else {
267+
sort.Slice(organizations, func(i, j int) bool {
268+
return strings.ToLower(organizations[i].Name) < strings.ToLower(organizations[j].Name)
269+
})
270+
271+
organizationNames := make([]string, len(organizations))
272+
for i, organization := range organizations {
273+
organizationNames[i] = organization.Name
274+
}
275+
276+
stop()
277+
278+
var organizationName string
279+
if err := survey.AskOne(&survey.Select{
280+
Message: "Which organization to use?",
281+
Options: organizationNames,
282+
Default: organizationNames[0],
283+
Description: func(_ string, idx int) string {
284+
return organizations[idx].ID
285+
},
286+
}, &organizationName, opts.IO.SurveyIO()); err != nil {
287+
return err
288+
}
289+
290+
for i, organization := range organizations {
291+
if organization.Name == organizationName {
292+
opts.OrganizationID = organizations[i].ID
293+
break
294+
}
295+
}
330296
}
331297
}
332298

@@ -342,11 +308,11 @@ func autoLogin(ctx context.Context, opts *loginOptions) error {
342308

343309
// Just use "cloud" as the alias if this is their first deployment and they
344310
// are authenticating against Axiom Cloud.
345-
if hostRef == strings.ToLower(typeCloud) {
311+
if hostRef == "cloud" {
346312
opts.Alias = hostRef
347313
}
348314

349-
// 4. Ask for an alias to use.
315+
// 3. Ask for an alias to use.
350316
if opts.Alias == "" {
351317
if err := survey.AskOne(&survey.Input{
352318
Message: "Under which name should the deployment be referenced in the future?",
@@ -407,27 +373,17 @@ func runLogin(ctx context.Context, opts *loginOptions) error {
407373
if opts.IO.IsStderrTTY() {
408374
cs := opts.IO.ColorScheme()
409375

410-
if user != nil {
411-
if (client.IsCloudURL(opts.URL) || opts.Config.ForceCloud) && client.IsPersonalToken(opts.Token) {
412-
organization, err := axiomClient.Organizations.Get(ctx, opts.OrganizationID)
413-
if err != nil {
414-
return err
415-
}
416-
417-
fmt.Fprintf(opts.IO.ErrOut(), "%s Logged in to organization %s as %s\n",
418-
cs.SuccessIcon(), cs.Bold(organization.Name), cs.Bold(user.Name))
419-
} else {
420-
fmt.Fprintf(opts.IO.ErrOut(), "%s Logged in to deployment %s as %s\n",
421-
cs.SuccessIcon(), cs.Bold(opts.Alias), cs.Bold(user.Name))
376+
if client.IsPersonalToken(opts.Token) {
377+
organization, err := axiomClient.Organizations.Get(ctx, opts.OrganizationID)
378+
if err != nil {
379+
return err
422380
}
381+
382+
fmt.Fprintf(opts.IO.ErrOut(), "%s Logged in to organization %s as %s\n",
383+
cs.SuccessIcon(), cs.Bold(organization.Name), cs.Bold(user.Name))
423384
} else {
424-
if client.IsCloudURL(opts.URL) || opts.Config.ForceCloud {
425-
fmt.Fprintf(opts.IO.ErrOut(), "%s Logged in to organization %s %s\n",
426-
cs.SuccessIcon(), cs.Bold(opts.OrganizationID), cs.Red(cs.Bold("(ingestion/query only!)")))
427-
} else {
428-
fmt.Fprintf(opts.IO.ErrOut(), "%s Logged in to deployment %s %s\n",
429-
cs.SuccessIcon(), cs.Bold(opts.Alias), cs.Red(cs.Bold("(ingestion/query only!)")))
430-
}
385+
fmt.Fprintf(opts.IO.ErrOut(), "%s Logged in to organization %s %s\n",
386+
cs.SuccessIcon(), cs.Bold(opts.OrganizationID), cs.Red(cs.Bold("(ingestion/query only!)")))
431387
}
432388
}
433389

internal/cmd/auth/auth_switch_org.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ func newSwitchOrgCmd(f *cmdutil.Factory) *cobra.Command {
4444
cmdutil.AsksForSetup(f, NewLoginCmd(f)),
4545
cmdutil.NeedsDeployments(f),
4646
cmdutil.NeedsActiveDeployment(f),
47-
cmdutil.NeedsCloudDeployment(f),
4847
cmdutil.NeedsPersonalAccessToken(f),
4948
),
5049

internal/cmd/auth/auth_update_token.go

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func newUpdateTokenCmd(f *cmdutil.Factory) *cobra.Command {
4040
$ axiom auth update-token
4141
4242
# Provide parameters on the command-line:
43-
$ echo $AXIOM_PERSONAL_ACCESS_TOKEN | axiom auth update-token
43+
$ echo $AXIOM_TOKEN | axiom auth update-token
4444
`),
4545

4646
PersistentPreRunE: cmdutil.ChainRunFuncs(
@@ -122,27 +122,17 @@ func runUpdateToken(ctx context.Context, opts *updateTokenOptions) error {
122122
if opts.IO.IsStderrTTY() {
123123
cs := opts.IO.ColorScheme()
124124

125-
if user != nil {
126-
if client.IsCloudURL(activeDeployment.URL) || opts.Config.ForceCloud {
127-
organization, err := axiomClient.Organizations.Get(ctx, activeDeployment.OrganizationID)
128-
if err != nil {
129-
return err
130-
}
131-
132-
fmt.Fprintf(opts.IO.ErrOut(), "%s Logged in to organization %s as %s\n",
133-
cs.SuccessIcon(), cs.Bold(organization.Name), cs.Bold(user.Name))
134-
} else {
135-
fmt.Fprintf(opts.IO.ErrOut(), "%s Logged in to deployment %s as %s\n",
136-
cs.SuccessIcon(), cs.Bold(opts.Config.ActiveDeployment), cs.Bold(user.Name))
125+
if client.IsPersonalToken(opts.Token) {
126+
organization, err := axiomClient.Organizations.Get(ctx, activeDeployment.OrganizationID)
127+
if err != nil {
128+
return err
137129
}
130+
131+
fmt.Fprintf(opts.IO.ErrOut(), "%s Logged in to organization %s as %s\n",
132+
cs.SuccessIcon(), cs.Bold(organization.Name), cs.Bold(user.Name))
138133
} else {
139-
if client.IsCloudURL(activeDeployment.URL) || opts.Config.ForceCloud {
140-
fmt.Fprintf(opts.IO.ErrOut(), "%s Logged in to organization %s %s\n",
141-
cs.SuccessIcon(), cs.Bold(activeDeployment.OrganizationID), cs.Red(cs.Bold("(ingestion only!)")))
142-
} else {
143-
fmt.Fprintf(opts.IO.ErrOut(), "%s Logged in to deployment %s %s\n",
144-
cs.SuccessIcon(), cs.Bold(opts.Config.ActiveDeployment), cs.Red(cs.Bold("(ingestion only!)")))
145-
}
134+
fmt.Fprintf(opts.IO.ErrOut(), "%s Logged in to organization %s %s\n",
135+
cs.SuccessIcon(), cs.Bold(activeDeployment.OrganizationID), cs.Red(cs.Bold("(ingestion/query only!)")))
146136
}
147137
}
148138

0 commit comments

Comments
 (0)