Skip to content

Commit

Permalink
Merge pull request #61 from Azure/jose/spinner
Browse files Browse the repository at this point in the history
spinner: Ensure there are no logs/prints between Start and Stop
  • Loading branch information
blanquicet authored Feb 7, 2024
2 parents 76393e0 + 44df752 commit 34574f2
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
5 changes: 2 additions & 3 deletions cmd/check-apiserver-connectivity.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
var connCheckCmd = &cobra.Command{
Use: "check-apiserver-connectivity",
Short: "Check connectivity between the nodes and the Kubernetes API Server",
Args: cobra.NoArgs,
RunE: connCheckCmdRun,
SilenceUsage: true,
}
Expand All @@ -26,7 +27,6 @@ func init() {
}

func connCheckCmdRun(cmd *cobra.Command, args []string) error {
utils.DefaultSpinner.Start()
cred, err := utils.GetCredentials()
if err != nil {
return fmt.Errorf("failed to authenticate: %w", err)
Expand All @@ -53,15 +53,14 @@ func connCheckCmdRun(cmd *cobra.Command, args []string) error {
return fmt.Errorf("couldn't parse stdout of response message:\n%s", res.Stdout)
}
if ret != 0 {
fmt.Printf("\nConnectivity check: failed with returned value %d: %s\n",
fmt.Printf("Connectivity check: failed with returned value %d: %s\n",
ret, res.Stderr)

// Force the binary to return an exit code != 0 (forwarding command's
// return value). Useful if it is used in scripts.
os.Exit(ret)
}

utils.DefaultSpinner.Stop()
fmt.Println("Connectivity check: succeeded")

return nil
Expand Down
13 changes: 11 additions & 2 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var configCmd = &cobra.Command{
var showConfigCmd = &cobra.Command{
Use: "show",
Short: "Show the configuration",
Args: cobra.NoArgs,
RunE: showConfigCmdRun,
SilenceUsage: true,
}
Expand All @@ -35,6 +36,7 @@ var useNodeCmd = &cobra.Command{
var unsetCurrentNodeCmd = &cobra.Command{
Use: "unset-current-node",
Short: "Unset the current node in the configuration",
Args: cobra.NoArgs,
RunE: unsetCurrentNodeCmdRun,
SilenceUsage: true,
}
Expand All @@ -49,6 +51,7 @@ var unsetNodeCmd = &cobra.Command{
var unsetAllCmd = &cobra.Command{
Use: "unset-all",
Short: "Unset all nodes in the configuration",
Args: cobra.NoArgs,
RunE: unsetAllCmdRun,
SilenceUsage: true,
}
Expand Down Expand Up @@ -122,20 +125,27 @@ func importCmdCommand() *cobra.Command {
var clusterName string

virtualMachineScaleSetVMs := func() (map[string]*utils.VirtualMachineScaleSetVM, error) {
utils.DefaultSpinner.Start()
utils.DefaultSpinner.Suffix = " Importing..."

if subscriptionID != "" && resourceGroup != "" && clusterName != "" {
vms, err := utils.VirtualMachineScaleSetVMsViaAzureAPI(subscriptionID, resourceGroup, clusterName)
utils.DefaultSpinner.Stop()
if err != nil {
return nil, fmt.Errorf("getting VMSS VMs via Azure API: %w", err)
}
return vms, nil
}

vms, err := utils.VirtualMachineScaleSetVMsViaKubeconfig()
utils.DefaultSpinner.Stop()
if err != nil {
logrus.Warn("Could not get VMSS VMs via Kubernetes API")
logrus.Warnf("Please provide '--%s', '--%s' and '--%s' flags to get VMSS VMs via Azure API",
utils.SubscriptionIDKey, utils.ResourceGroupKey, utils.ClusterNameKey)
return nil, fmt.Errorf("getting VMSS VMs via Kuberntes API: %w", err)
}

return vms, nil
}

Expand All @@ -147,9 +157,8 @@ func importCmdCommand() *cobra.Command {
"In case of Azure API, you need to provide '--%s', '--%s' and '--%s' flags.",
utils.SubscriptionIDKey, utils.ResourceGroupKey, utils.ClusterNameKey),
SilenceUsage: true,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
utils.DefaultSpinner.Start()
defer utils.DefaultSpinner.Stop()
vms, err := virtualMachineScaleSetVMs()
if err != nil {
return err
Expand Down
2 changes: 0 additions & 2 deletions cmd/run-command.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func init() {
}

func runCommandCmdRun(cmd *cobra.Command, args []string) error {
utils.DefaultSpinner.Start()
cred, err := utils.GetCredentials()
if err != nil {
return fmt.Errorf("authenticating: %w", err)
Expand All @@ -75,7 +74,6 @@ func runCommandCmdRun(cmd *cobra.Command, args []string) error {
return fmt.Errorf("running command: %w", err)
}

utils.DefaultSpinner.Stop()
fmt.Fprintf(os.Stderr, "%s", res.Stderr)
fmt.Fprintf(os.Stdout, "%s", res.Stdout)
return nil
Expand Down
5 changes: 5 additions & 0 deletions cmd/utils/vmss.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,18 @@ func RunCommand(
os.Exit(1)
}()

DefaultSpinner.Start()
DefaultSpinner.Suffix = " Running..."

poller, err := client.BeginRunCommand(ctx, vm.NodeResourceGroup,
vm.VMScaleSet, vm.InstanceID, runCommand, nil)
if err != nil {
DefaultSpinner.Stop()
return nil, fmt.Errorf("begin running command: %w", err)
}

res, err := poller.PollUntilDone(ctx, &runtime.PollUntilDoneOptions{Frequency: pollingFreq})
DefaultSpinner.Stop()
if err != nil {
return nil, fmt.Errorf("polling command response: %w", err)
}
Expand Down
1 change: 1 addition & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func init() {
var versionCmd = &cobra.Command{
Use: "version",
Short: "Show version",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(version)
},
Expand Down

0 comments on commit 34574f2

Please sign in to comment.