2023-02-28 15:50:35 +01:00
|
|
|
package flag
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-03-07 23:10:42 +01:00
|
|
|
"os"
|
|
|
|
"strings"
|
2023-02-28 15:50:35 +01:00
|
|
|
|
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/format"
|
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/format/table"
|
2023-03-07 23:10:42 +01:00
|
|
|
"github.com/pkg/errors"
|
2023-02-28 15:50:35 +01:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
2023-08-25 20:02:02 +02:00
|
|
|
const (
|
|
|
|
AuthTokenDefaultHomePath = "$HOME/.config/emissary/auth-token"
|
|
|
|
AuthTokenDefaultLocalPath = ".emissary-token"
|
|
|
|
)
|
|
|
|
|
2023-02-28 15:50:35 +01:00
|
|
|
func ComposeFlags(flags ...cli.Flag) []cli.Flag {
|
|
|
|
baseFlags := []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "server",
|
|
|
|
Aliases: []string{"s"},
|
|
|
|
Usage: "use `SERVER` as server url",
|
|
|
|
Value: "http://127.0.0.1:3000",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "format",
|
|
|
|
Aliases: []string{"f"},
|
|
|
|
Usage: fmt.Sprintf("use `FORMAT` as output format (available: %s)", format.Available()),
|
|
|
|
Value: string(table.Format),
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "output-mode",
|
|
|
|
Aliases: []string{"m"},
|
|
|
|
Usage: fmt.Sprintf("use `MODE` as output mode (available: %s)", []format.OutputMode{format.OutputModeCompact, format.OutputModeWide}),
|
|
|
|
Value: string(format.OutputModeCompact),
|
|
|
|
},
|
2023-03-07 23:10:42 +01:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "token",
|
|
|
|
Aliases: []string{"t"},
|
2023-03-13 10:44:58 +01:00
|
|
|
Usage: "use `TOKEN` as authentication token",
|
2023-03-07 23:10:42 +01:00
|
|
|
},
|
2023-08-25 20:02:02 +02:00
|
|
|
&cli.StringSliceFlag{
|
2023-03-07 23:10:42 +01:00
|
|
|
Name: "token-file",
|
2023-03-13 10:44:58 +01:00
|
|
|
Usage: "use `TOKEN_FILE` as file containing the authentication token",
|
2023-08-25 20:02:02 +02:00
|
|
|
Value: cli.NewStringSlice(AuthTokenDefaultLocalPath, AuthTokenDefaultHomePath),
|
2023-03-07 23:10:42 +01:00
|
|
|
TakesFile: true,
|
|
|
|
},
|
2023-02-28 15:50:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
flags = append(flags, baseFlags...)
|
|
|
|
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
|
|
|
type BaseFlags struct {
|
|
|
|
ServerURL string
|
|
|
|
Format format.Format
|
|
|
|
OutputMode format.OutputMode
|
2023-03-07 23:10:42 +01:00
|
|
|
Token string
|
2023-08-25 20:02:02 +02:00
|
|
|
TokenFiles []string
|
2023-02-28 15:50:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetBaseFlags(ctx *cli.Context) *BaseFlags {
|
|
|
|
serverURL := ctx.String("server")
|
|
|
|
rawFormat := ctx.String("format")
|
|
|
|
rawOutputMode := ctx.String("output-mode")
|
2023-08-25 20:02:02 +02:00
|
|
|
tokenFiles := ctx.StringSlice("token-file")
|
2023-03-07 23:10:42 +01:00
|
|
|
token := ctx.String("token")
|
2023-02-28 15:50:35 +01:00
|
|
|
|
|
|
|
return &BaseFlags{
|
|
|
|
ServerURL: serverURL,
|
|
|
|
Format: format.Format(rawFormat),
|
|
|
|
OutputMode: format.OutputMode(rawOutputMode),
|
2023-03-07 23:10:42 +01:00
|
|
|
Token: token,
|
2023-08-25 20:02:02 +02:00
|
|
|
TokenFiles: tokenFiles,
|
2023-03-07 23:10:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetToken(flags *BaseFlags) (string, error) {
|
|
|
|
if flags.Token != "" {
|
|
|
|
return flags.Token, nil
|
2023-02-28 15:50:35 +01:00
|
|
|
}
|
2023-03-07 23:10:42 +01:00
|
|
|
|
2023-08-25 20:02:02 +02:00
|
|
|
for _, tokenFile := range flags.TokenFiles {
|
|
|
|
tokenFile = os.ExpandEnv(tokenFile)
|
2023-03-07 23:10:42 +01:00
|
|
|
|
2023-08-25 20:02:02 +02:00
|
|
|
rawToken, err := os.ReadFile(tokenFile)
|
|
|
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
|
|
return "", errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rawToken == nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-03-07 23:10:42 +01:00
|
|
|
|
2023-08-25 20:02:02 +02:00
|
|
|
return strings.TrimSpace(string(rawToken)), nil
|
2023-03-07 23:10:42 +01:00
|
|
|
}
|
|
|
|
|
2023-08-25 20:02:02 +02:00
|
|
|
return "", nil
|
2023-02-28 15:50:35 +01:00
|
|
|
}
|