Skip to content

Commit

Permalink
improve errors and streamline cli error output
Browse files Browse the repository at this point in the history
Signed-off-by: Kristoffer Dalby <[email protected]>
  • Loading branch information
kradalby committed Sep 6, 2024
1 parent 42f9cdc commit 9a09cee
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 152 deletions.
25 changes: 0 additions & 25 deletions cmd/headscale/cli/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,10 @@ var listAPIKeys = &cobra.Command{
fmt.Sprintf("Error getting the list of keys: %s", err),
output,
)

return
}

if output != "" {
SuccessOutput(response.GetApiKeys(), "", output)

return
}

tableData := pterm.TableData{
Expand Down Expand Up @@ -102,8 +98,6 @@ var listAPIKeys = &cobra.Command{
fmt.Sprintf("Failed to render pterm table: %s", err),
output,
)

return
}
},
}
Expand All @@ -119,9 +113,6 @@ If you loose a key, create a new one and revoke (expire) the old one.`,
Run: func(cmd *cobra.Command, args []string) {
output, _ := cmd.Flags().GetString("output")

log.Trace().
Msg("Preparing to create ApiKey")

request := &v1.CreateApiKeyRequest{}

durationStr, _ := cmd.Flags().GetString("expiration")
Expand All @@ -133,16 +124,10 @@ If you loose a key, create a new one and revoke (expire) the old one.`,
fmt.Sprintf("Could not parse duration: %s\n", err),
output,
)

return
}

expiration := time.Now().UTC().Add(time.Duration(duration))

log.Trace().
Dur("expiration", time.Duration(duration)).
Msg("expiration has been set")

request.Expiration = timestamppb.New(expiration)

ctx, client, conn, cancel := newHeadscaleCLIWithConfig()
Expand All @@ -156,8 +141,6 @@ If you loose a key, create a new one and revoke (expire) the old one.`,
fmt.Sprintf("Cannot create Api Key: %s\n", err),
output,
)

return
}

SuccessOutput(response.GetApiKey(), response.GetApiKey(), output)
Expand All @@ -178,8 +161,6 @@ var expireAPIKeyCmd = &cobra.Command{
fmt.Sprintf("Error getting prefix from CLI flag: %s", err),
output,
)

return
}

ctx, client, conn, cancel := newHeadscaleCLIWithConfig()
Expand All @@ -197,8 +178,6 @@ var expireAPIKeyCmd = &cobra.Command{
fmt.Sprintf("Cannot expire Api Key: %s\n", err),
output,
)

return
}

SuccessOutput(response, "Key expired", output)
Expand All @@ -219,8 +198,6 @@ var deleteAPIKeyCmd = &cobra.Command{
fmt.Sprintf("Error getting prefix from CLI flag: %s", err),
output,
)

return
}

ctx, client, conn, cancel := newHeadscaleCLIWithConfig()
Expand All @@ -238,8 +215,6 @@ var deleteAPIKeyCmd = &cobra.Command{
fmt.Sprintf("Cannot delete Api Key: %s\n", err),
output,
)

return
}

SuccessOutput(response, "Key deleted", output)
Expand Down
12 changes: 0 additions & 12 deletions cmd/headscale/cli/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ var createNodeCmd = &cobra.Command{
user, err := cmd.Flags().GetString("user")
if err != nil {
ErrorOutput(err, fmt.Sprintf("Error getting user: %s", err), output)

return
}

ctx, client, conn, cancel := newHeadscaleCLIWithConfig()
Expand All @@ -79,8 +77,6 @@ var createNodeCmd = &cobra.Command{
fmt.Sprintf("Error getting node from flag: %s", err),
output,
)

return
}

machineKey, err := cmd.Flags().GetString("key")
Expand All @@ -90,8 +86,6 @@ var createNodeCmd = &cobra.Command{
fmt.Sprintf("Error getting key from flag: %s", err),
output,
)

return
}

var mkey key.MachinePublic
Expand All @@ -102,8 +96,6 @@ var createNodeCmd = &cobra.Command{
fmt.Sprintf("Failed to parse machine key from flag: %s", err),
output,
)

return
}

routes, err := cmd.Flags().GetStringSlice("route")
Expand All @@ -113,8 +105,6 @@ var createNodeCmd = &cobra.Command{
fmt.Sprintf("Error getting routes from flag: %s", err),
output,
)

return
}

request := &v1.DebugCreateNodeRequest{
Expand All @@ -131,8 +121,6 @@ var createNodeCmd = &cobra.Command{
fmt.Sprintf("Cannot create node: %s", status.Convert(err).Message()),
output,
)

return
}

SuccessOutput(response.GetNode(), "Node created", output)
Expand Down
18 changes: 0 additions & 18 deletions cmd/headscale/cli/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ var registerNodeCmd = &cobra.Command{
user, err := cmd.Flags().GetString("user")
if err != nil {
ErrorOutput(err, fmt.Sprintf("Error getting user: %s", err), output)

return
}

ctx, client, conn, cancel := newHeadscaleCLIWithConfig()
Expand All @@ -131,8 +129,6 @@ var registerNodeCmd = &cobra.Command{
fmt.Sprintf("Error getting node key from flag: %s", err),
output,
)

return
}

request := &v1.RegisterNodeRequest{
Expand All @@ -150,8 +146,6 @@ var registerNodeCmd = &cobra.Command{
),
output,
)

return
}

SuccessOutput(
Expand All @@ -169,14 +163,10 @@ var listNodesCmd = &cobra.Command{
user, err := cmd.Flags().GetString("user")
if err != nil {
ErrorOutput(err, fmt.Sprintf("Error getting user: %s", err), output)

return
}
showTags, err := cmd.Flags().GetBool("tags")
if err != nil {
ErrorOutput(err, fmt.Sprintf("Error getting tags flag: %s", err), output)

return
}

ctx, client, conn, cancel := newHeadscaleCLIWithConfig()
Expand All @@ -194,21 +184,15 @@ var listNodesCmd = &cobra.Command{
fmt.Sprintf("Cannot get nodes: %s", status.Convert(err).Message()),
output,
)

return
}

if output != "" {
SuccessOutput(response.GetNodes(), "", output)

return
}

tableData, err := nodesToPtables(user, showTags, response.GetNodes())
if err != nil {
ErrorOutput(err, fmt.Sprintf("Error converting to table: %s", err), output)

return
}

err = pterm.DefaultTable.WithHasHeader().WithData(tableData).Render()
Expand All @@ -218,8 +202,6 @@ var listNodesCmd = &cobra.Command{
fmt.Sprintf("Failed to render pterm table: %s", err),
output,
)

return
}
},
}
Expand Down
23 changes: 10 additions & 13 deletions cmd/headscale/cli/policy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"fmt"
"io"
"os"

Expand Down Expand Up @@ -30,6 +31,7 @@ var getPolicy = &cobra.Command{
Short: "Print the current ACL Policy",
Aliases: []string{"show", "view", "fetch"},
Run: func(cmd *cobra.Command, args []string) {
output, _ := cmd.Flags().GetString("output")
ctx, client, conn, cancel := newHeadscaleCLIWithConfig()
defer cancel()
defer conn.Close()
Expand All @@ -38,13 +40,13 @@ var getPolicy = &cobra.Command{

response, err := client.GetPolicy(ctx, request)
if err != nil {
log.Fatal().Err(err).Msg("Failed to get the policy")

return
ErrorOutput(err, fmt.Sprintf("Failed loading ACL Policy: %s", err), output)
}

// TODO(pallabpain): Maybe print this better?
SuccessOutput("", response.GetPolicy(), "hujson")
// This does not pass output as we dont support yaml, json or json-line
// output for this command. It is HuJSON already.
SuccessOutput("", response.GetPolicy(), "")
},
}

Expand All @@ -56,21 +58,18 @@ var setPolicy = &cobra.Command{
This command only works when the acl.policy_mode is set to "db", and the policy will be stored in the database.`,
Aliases: []string{"put", "update"},
Run: func(cmd *cobra.Command, args []string) {
output, _ := cmd.Flags().GetString("output")
policyPath, _ := cmd.Flags().GetString("file")

f, err := os.Open(policyPath)
if err != nil {
log.Fatal().Err(err).Msg("Error opening the policy file")

return
ErrorOutput(err, fmt.Sprintf("Error opening the policy file: %s", err), output)
}
defer f.Close()

policyBytes, err := io.ReadAll(f)
if err != nil {
log.Fatal().Err(err).Msg("Error reading the policy file")

return
ErrorOutput(err, fmt.Sprintf("Error reading the policy file: %s", err), output)
}

request := &v1.SetPolicyRequest{Policy: string(policyBytes)}
Expand All @@ -80,9 +79,7 @@ var setPolicy = &cobra.Command{
defer conn.Close()

if _, err := client.SetPolicy(ctx, request); err != nil {
log.Fatal().Err(err).Msg("Failed to set ACL Policy")

return
ErrorOutput(err, fmt.Sprintf("Failed to set ACL Policy: %s", err), output)
}

SuccessOutput(nil, "Policy updated.", "")
Expand Down
22 changes: 0 additions & 22 deletions cmd/headscale/cli/preauthkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ var listPreAuthKeys = &cobra.Command{
user, err := cmd.Flags().GetString("user")
if err != nil {
ErrorOutput(err, fmt.Sprintf("Error getting user: %s", err), output)

return
}

ctx, client, conn, cancel := newHeadscaleCLIWithConfig()
Expand All @@ -85,8 +83,6 @@ var listPreAuthKeys = &cobra.Command{

if output != "" {
SuccessOutput(response.GetPreAuthKeys(), "", output)

return
}

tableData := pterm.TableData{
Expand Down Expand Up @@ -134,8 +130,6 @@ var listPreAuthKeys = &cobra.Command{
fmt.Sprintf("Failed to render pterm table: %s", err),
output,
)

return
}
},
}
Expand All @@ -150,20 +144,12 @@ var createPreAuthKeyCmd = &cobra.Command{
user, err := cmd.Flags().GetString("user")
if err != nil {
ErrorOutput(err, fmt.Sprintf("Error getting user: %s", err), output)

return
}

reusable, _ := cmd.Flags().GetBool("reusable")
ephemeral, _ := cmd.Flags().GetBool("ephemeral")
tags, _ := cmd.Flags().GetStringSlice("tags")

log.Trace().
Bool("reusable", reusable).
Bool("ephemeral", ephemeral).
Str("user", user).
Msg("Preparing to create preauthkey")

request := &v1.CreatePreAuthKeyRequest{
User: user,
Reusable: reusable,
Expand All @@ -180,8 +166,6 @@ var createPreAuthKeyCmd = &cobra.Command{
fmt.Sprintf("Could not parse duration: %s\n", err),
output,
)

return
}

expiration := time.Now().UTC().Add(time.Duration(duration))
Expand All @@ -203,8 +187,6 @@ var createPreAuthKeyCmd = &cobra.Command{
fmt.Sprintf("Cannot create Pre Auth Key: %s\n", err),
output,
)

return
}

SuccessOutput(response.GetPreAuthKey(), response.GetPreAuthKey().GetKey(), output)
Expand All @@ -227,8 +209,6 @@ var expirePreAuthKeyCmd = &cobra.Command{
user, err := cmd.Flags().GetString("user")
if err != nil {
ErrorOutput(err, fmt.Sprintf("Error getting user: %s", err), output)

return
}

ctx, client, conn, cancel := newHeadscaleCLIWithConfig()
Expand All @@ -247,8 +227,6 @@ var expirePreAuthKeyCmd = &cobra.Command{
fmt.Sprintf("Cannot expire Pre Auth Key: %s\n", err),
output,
)

return
}

SuccessOutput(response, "Key expired", output)
Expand Down
9 changes: 4 additions & 5 deletions cmd/headscale/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,10 @@ func initConfig() {
zerolog.SetGlobalLevel(zerolog.Disabled)
}

logFormat := viper.GetString("log.format")

if logFormat == types.JSONLogFormat {
log.Logger = log.Output(os.Stdout)
}
// logFormat := viper.GetString("log.format")
// if logFormat == types.JSONLogFormat {
// log.Logger = log.Output(os.Stdout)
// }

disableUpdateCheck := viper.GetBool("disable_check_updates")
if !disableUpdateCheck && !machineOutput {
Expand Down
Loading

0 comments on commit 9a09cee

Please sign in to comment.