-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmd/azure/who_am_i): Create
slu az who-am-i
to get current sub…
…scription
- Loading branch information
1 parent
2fa345e
commit 0f6ce9d
Showing
4 changed files
with
103 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package password | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
parent_cmd "github.com/sikalabs/slu/cmd/azure" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/Azure/azure-sdk-for-go/profiles/2017-03-09/resources/mgmt/subscriptions" | ||
"github.com/Azure/go-autorest/autorest/azure/auth" | ||
) | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: "who-am-i", | ||
Short: "Get Azure Account Info", | ||
Aliases: []string{"w"}, | ||
Args: cobra.NoArgs, | ||
Run: func(c *cobra.Command, args []string) { | ||
printSubscriptionInfo() | ||
}, | ||
} | ||
|
||
func init() { | ||
parent_cmd.Cmd.AddCommand(Cmd) | ||
} | ||
|
||
func printSubscriptionInfo() { | ||
// Use environment variables for authentication | ||
authorizer, err := auth.NewAuthorizerFromCLI() | ||
handleError(err) | ||
|
||
// Create a client | ||
subscriptionsClient := subscriptions.NewClient() | ||
subscriptionsClient.Authorizer = authorizer | ||
|
||
// Get the list of subscriptions | ||
subList, err := subscriptionsClient.List(context.Background()) | ||
handleError(err) | ||
|
||
for _, sub := range subList.Values() { | ||
if sub.State == subscriptions.Enabled { | ||
fmt.Printf("Subscription ID: %s\n", *sub.SubscriptionID) | ||
fmt.Printf("Subscription Name: %s\n", *sub.DisplayName) | ||
return | ||
} | ||
} | ||
|
||
log.Fatalln("No active subscription found") | ||
} | ||
|
||
func handleError(err error) { | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
} |
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
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