-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nsc auth: introduce generate-dev-token to generate short-lived tokens…
… for development fixes BKC-56
- Loading branch information
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2022 Namespace Labs Inc; All rights reserved. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
|
||
package auth | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
"namespacelabs.dev/foundation/internal/cli/fncobra" | ||
"namespacelabs.dev/foundation/internal/console" | ||
"namespacelabs.dev/foundation/internal/fnapi" | ||
"namespacelabs.dev/foundation/internal/fnerrors" | ||
) | ||
|
||
func NewGenerateDevTokenCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "generate-dev-token", | ||
Short: "Generate a Namespace Cloud token for development purposes.", | ||
Args: cobra.NoArgs, | ||
Hidden: true, | ||
} | ||
|
||
outputPath := cmd.Flags().String("output_to", "", "If specified, write the access token to this path.") | ||
|
||
return fncobra.Cmd(cmd).Do(func(ctx context.Context) error { | ||
res, err := fnapi.IssueDevelopmentToken(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if *outputPath != "" { | ||
if err := os.WriteFile(*outputPath, []byte(res.DevelopmentToken), 0644); err != nil { | ||
return fnerrors.New("failed to write %q: %w", *outputPath, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
fmt.Fprintln(console.Stdout(ctx), res.DevelopmentToken) | ||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters