|
| 1 | +// Copyright © 2018 Humio Ltd. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package cmd |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "io/ioutil" |
| 20 | + "os" |
| 21 | + |
| 22 | + "github.com/spf13/cobra" |
| 23 | +) |
| 24 | + |
| 25 | +// usersCmd represents the users command |
| 26 | +func newLicenseInstallCmd() *cobra.Command { |
| 27 | + var license string |
| 28 | + |
| 29 | + cmd := &cobra.Command{ |
| 30 | + Use: "install [flags] (<license-file> | --license=<string>)", |
| 31 | + Short: "Install a Humio license", |
| 32 | + Run: func(cmd *cobra.Command, args []string) { |
| 33 | + if len(args) == 1 { |
| 34 | + filepath := args[0] |
| 35 | + |
| 36 | + licenseBytes, readErr := ioutil.ReadFile(filepath) |
| 37 | + if readErr != nil { |
| 38 | + fmt.Println(fmt.Errorf("error reading license file: %s", readErr)) |
| 39 | + os.Exit(1) |
| 40 | + } |
| 41 | + |
| 42 | + license = string(licenseBytes) |
| 43 | + } else if license != "" { |
| 44 | + // License set from flag |
| 45 | + } else { |
| 46 | + fmt.Println("Expected either an argument <filename> or flag --license=<license>.") |
| 47 | + cmd.Help() |
| 48 | + os.Exit(1) |
| 49 | + } |
| 50 | + |
| 51 | + client := NewApiClient(cmd) |
| 52 | + installErr := client.License().Install(license) |
| 53 | + |
| 54 | + if installErr != nil { |
| 55 | + fmt.Println(fmt.Errorf("error installing license: %s", installErr)) |
| 56 | + os.Exit(1) |
| 57 | + } |
| 58 | + |
| 59 | + fmt.Println("License installed") |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + cmd.Flags().StringVarP(&license, "license", "l", "", "A string with the content license license file.") |
| 64 | + |
| 65 | + return cmd |
| 66 | +} |
0 commit comments