Skip to content

Commit

Permalink
Move things around to reduce symbols in main package
Browse files Browse the repository at this point in the history
  • Loading branch information
punmechanic committed Nov 11, 2024
1 parent c693c4b commit 27f8b2a
Show file tree
Hide file tree
Showing 29 changed files with 54 additions and 40 deletions.
2 changes: 1 addition & 1 deletion cli/accounts.go → command/accounts.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion cli/alias.go → command/alias.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"github.com/spf13/cobra"
Expand Down
2 changes: 1 addition & 1 deletion cli/awsconfig.go → command/awsconfig.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"os"
Expand Down
2 changes: 1 addition & 1 deletion cli/awsconfig_test.go → command/awsconfig_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion cli/config.go → command/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"encoding/csv"
Expand Down
2 changes: 1 addition & 1 deletion cli/config_test.go → command/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"encoding/json"
Expand Down
2 changes: 1 addition & 1 deletion cli/consts.go → command/consts.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

// Vars for build time
var (
Expand Down
2 changes: 1 addition & 1 deletion cli/context.go → command/context.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion cli/credentials.go → command/credentials.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion cli/credentials_test.go → command/credentials_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"testing"
Expand Down
10 changes: 9 additions & 1 deletion cli/error.go → command/error.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"errors"
Expand Down Expand Up @@ -168,3 +168,11 @@ func tryParseTimeToLiveError(err error) (error, bool) {

return nil, false
}

func GetExitCode(err error) (int, bool) {
var codeError codeError
if errors.As(err, &codeError) {
return codeError.Code(), true
}
return 0, false
}
2 changes: 1 addition & 1 deletion cli/error_test.go → command/error_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion cli/get.go → command/get.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion cli/html.go → command/html.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion cli/html_test.go → command/html_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"strings"
Expand Down
2 changes: 1 addition & 1 deletion cli/log.go → command/log.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"net/http"
Expand Down
2 changes: 1 addition & 1 deletion cli/login.go → command/login.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion cli/login_test.go → command/login_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion cli/oauth2.go → command/oauth2.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion cli/oauth2_test.go → command/oauth2_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"net/http"
Expand Down
2 changes: 1 addition & 1 deletion cli/roles.go → command/roles.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"github.com/spf13/cobra"
Expand Down
10 changes: 9 additions & 1 deletion cli/root.go → command/root.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"context"
Expand All @@ -8,6 +8,7 @@ import (
"runtime"
"time"

"github.com/coreos/go-oidc"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -103,3 +104,10 @@ To get started run the following commands:
SilenceErrors: true,
SilenceUsage: true,
}

func Execute(ctx context.Context, args []string) error {
client := NewHTTPClient()
ctx = oidc.ClientContext(ctx, client)
rootCmd.SetArgs(args)
return rootCmd.ExecuteContext(ctx)
}
2 changes: 1 addition & 1 deletion cli/root_test.go → command/root_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion cli/saml.go → command/saml.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"strings"
Expand Down
2 changes: 1 addition & 1 deletion cli/saml_test.go → command/saml_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion cli/set.go → command/set.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion cli/switch.go → command/switch.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion cli/unalias.go → command/unalias.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package command

import (
"github.com/spf13/cobra"
Expand Down
22 changes: 10 additions & 12 deletions cli/main.go → main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"log/slog"

"github.com/coreos/go-oidc"
"github.com/riotgames/key-conjurer/command"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -42,23 +42,21 @@ func main() {
if flag, ok := os.LookupEnv("KEYCONJURERFLAGS"); ok {
args = append(args, strings.Split(flag, " ")...)
}
rootCmd.SetArgs(args)

ctx := oidc.ClientContext(context.Background(), NewHTTPClient())
err := rootCmd.ExecuteContext(ctx)
err := command.Execute(context.Background(), args)
if IsWindowsPortAccessError(err) {
fmt.Fprintf(os.Stderr, "Encountered an issue when opening the port for KeyConjurer: %s\n", err)
fmt.Fprintln(os.Stderr, "Consider running `net stop hns` and then `net start hns`")
os.Exit(ExitCodeConnectivityError)
os.Exit(command.ExitCodeConnectivityError)
}

var codeErr codeError
if errors.As(err, &codeErr) {
cobra.CheckErr(codeErr)
os.Exit(int(codeErr.Code()))
} else if err != nil {
// Probably a cobra error.
if err != nil {
cobra.CheckErr(err)
os.Exit(ExitCodeUnknownError)

errorCode, ok := command.GetExitCode(err)
if !ok {
errorCode = command.ExitCodeUnknownError
}
os.Exit(errorCode)
}
}

0 comments on commit 27f8b2a

Please sign in to comment.