|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + |
| 10 | + // "strings" |
| 11 | + |
| 12 | + "onyxia-cli/src/api" |
| 13 | + . "onyxia-cli/src/configuration" |
| 14 | + "onyxia-cli/src/oidc" |
| 15 | + |
| 16 | + "github.com/urfave/cli/v2" |
| 17 | +) |
| 18 | + |
| 19 | +func main() { |
| 20 | + ReadConfig() |
| 21 | + // Disable SSL validation |
| 22 | + http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} |
| 23 | + |
| 24 | + app := &cli.App{ |
| 25 | + Version: "v0.1", |
| 26 | + Name: "onyxia-cli", |
| 27 | + Usage: "CLI for Onyxia services", |
| 28 | + EnableBashCompletion: true, |
| 29 | + Flags: []cli.Flag{ |
| 30 | + &cli.StringFlag{ |
| 31 | + Name: "accessToken", |
| 32 | + Aliases: []string{"token"}, |
| 33 | + EnvVars: []string{"KC_ACCESS_TOKEN"}, |
| 34 | + Required: true, |
| 35 | + }, |
| 36 | + }, |
| 37 | + Commands: []*cli.Command{ |
| 38 | + { |
| 39 | + Name: "whoami", |
| 40 | + Usage: "Show user's informations", |
| 41 | + Action: func(c *cli.Context) error { |
| 42 | + tokenString := c.String("accessToken") |
| 43 | + oidc.DisplayID(tokenString) |
| 44 | + return nil |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + Name: "kub", |
| 49 | + Usage: "Setup kubernetes", |
| 50 | + Subcommands: []*cli.Command{ |
| 51 | + { |
| 52 | + Name: "create", |
| 53 | + Usage: "Create resources", |
| 54 | + Subcommands: []*cli.Command{ |
| 55 | + { |
| 56 | + Name: "namespace", |
| 57 | + Usage: "Create namespace", |
| 58 | + Flags: []cli.Flag{ |
| 59 | + &cli.StringFlag{ |
| 60 | + Name: "namespaceName", |
| 61 | + Aliases: []string{"name", "n"}, |
| 62 | + Usage: "Name of the namespace to create", |
| 63 | + Required: true, |
| 64 | + }, |
| 65 | + &cli.StringFlag{ |
| 66 | + Name: "namespaceOwnerType", |
| 67 | + Aliases: []string{"ownerType", "t"}, |
| 68 | + Required: false, |
| 69 | + Usage: "The type of ownership. Can be either USER or GROUP.", |
| 70 | + DefaultText: "USER", |
| 71 | + Value: "USER", |
| 72 | + }, |
| 73 | + &cli.StringFlag{ |
| 74 | + Name: "namespaceOwner", |
| 75 | + Aliases: []string{"owner", "o"}, |
| 76 | + Required: false, |
| 77 | + Usage: "The owner of the namespace.", |
| 78 | + DefaultText: "YOU", |
| 79 | + }, |
| 80 | + }, |
| 81 | + Action: func(c *cli.Context) error { |
| 82 | + namespaceName := c.String("namespaceName") |
| 83 | + tokenString := c.String("accessToken") |
| 84 | + ownerType := c.String("namespaceOwnerType") |
| 85 | + ownerName := c.String("namespaceOwner") |
| 86 | + if ownerType == "USER" { |
| 87 | + api.CreateNamespace(tokenString, namespaceName, ownerType, "oidc"+oidc.GetID(tokenString).Preferred_username) |
| 88 | + } else { |
| 89 | + if ownerName == "" { |
| 90 | + fmt.Println("Veuillez choisir un groupe parmi ceux-ci :") |
| 91 | + oidc.DisplayGroups(oidc.GetID(tokenString), false) |
| 92 | + } else { |
| 93 | + api.CreateNamespace(tokenString, namespaceName, ownerType, "oidc"+ownerName) |
| 94 | + } |
| 95 | + } |
| 96 | + return nil |
| 97 | + }, |
| 98 | + BashComplete: func(c *cli.Context) { |
| 99 | + if c.NArg() > 0 { |
| 100 | + return |
| 101 | + } |
| 102 | + if os.Args[len(os.Args)-2] == "-o" || os.Args[len(os.Args)-2] == "--owner" { |
| 103 | + oidc.DisplayGroups(oidc.GetID(c.String("accessToken")), true) |
| 104 | + } else if os.Args[len(os.Args)-2] == "-t" || os.Args[len(os.Args)-2] == "--ownerType" { |
| 105 | + for _, elem := range []string{"GROUP", "USER"} { |
| 106 | + fmt.Println(elem) |
| 107 | + } |
| 108 | + } |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + } |
| 117 | + |
| 118 | + err := app.Run(os.Args) |
| 119 | + if err != nil { |
| 120 | + log.Fatal(err) |
| 121 | + } |
| 122 | +} |
0 commit comments