Run initializes the root command and executes it. It also handles errors and prints them in a user-friendly way. NOTE: If you change this function, also check if you need to update testcli.Fixture.Run.
(ctx context.Context, ver version.Version)
| 57 | // It also handles errors and prints them in a user-friendly way. |
| 58 | // NOTE: If you change this function, also check if you need to update testcli.Fixture.Run. |
| 59 | func Run(ctx context.Context, ver version.Version) { |
| 60 | ch, err := cmdutil.NewHelper(ver, "") |
| 61 | if err != nil { |
| 62 | fmt.Printf("Error: %v\n", err) |
| 63 | os.Exit(1) |
| 64 | } |
| 65 | |
| 66 | // Crude check to detect if we may be running as Rill Cloud, namely if the command is `rill admin ...` or `rill runtime ...`. |
| 67 | isCloud := len(os.Args) >= 2 && (os.Args[1] == "admin" || os.Args[1] == "runtime") |
| 68 | |
| 69 | // Check version (if not running as a cloud service). |
| 70 | // NOTE: Not using PersistentPreRunE due to this issue: https://github.com/spf13/cobra/issues/216. |
| 71 | if !isCloud { |
| 72 | err = ch.CheckVersion(ctx) |
| 73 | if err != nil { |
| 74 | ch.PrintfWarn("Warning: version check failed: %v\n\n", err) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Print warning if currently acting as an assumed user |
| 79 | representingUser, err := ch.DotRill.GetRepresentingUser() |
| 80 | if err != nil { |
| 81 | ch.PrintfWarn("Could not parse representing user email: %v\n\n", err) |
| 82 | } |
| 83 | if representingUser != "" { |
| 84 | expiryTime, err := ch.DotRill.GetRepresentingUserAccessTokenExpiry() |
| 85 | if err != nil { |
| 86 | ch.PrintfWarn("Could not parse token expiry %v\n\n", err) |
| 87 | } else if time.Now().After(expiryTime) { |
| 88 | // If the assumed user's token has expired, silently unassume and revert to the original user before executing the command. |
| 89 | err := sudouser.UnassumeUser(ctx, ch) |
| 90 | if err != nil { |
| 91 | ch.PrintfWarn("Could not unassume user after the token expired: %v\n\n", err) |
| 92 | } |
| 93 | } else { |
| 94 | ch.PrintfWarn("Warning: Running action as %q\n\n", representingUser) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // Execute the root command |
| 99 | err = RootCmd(ch).ExecuteContext(ctx) |
| 100 | code := HandleExecuteError(ch, err) |
| 101 | ch.Close() |
| 102 | os.Exit(code) |
| 103 | } |
| 104 | |
| 105 | // RootCmd creates the root command and adds all subcommands. |
| 106 | func RootCmd(ch *cmdutil.Helper) *cobra.Command { |
no test coverage detected