Skip to content

Commit b3bf727

Browse files
committed
Add license command
1 parent b9072fc commit b3bf727

5 files changed

Lines changed: 207 additions & 0 deletions

File tree

api/license.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package api
2+
3+
import (
4+
"github.com/shurcooL/graphql"
5+
)
6+
7+
type License struct {
8+
client *Client
9+
}
10+
11+
type LicenseData struct {
12+
ExpiresAt string
13+
IssuedAt string
14+
}
15+
16+
func (c *Client) License() *License { return &License{client: c} }
17+
18+
func (p *License) Install(license string) error {
19+
20+
var mutation struct {
21+
CreateParser struct {
22+
Type string `graphql:"__typename"`
23+
} `graphql:"updateLicenseKey(license: $license)"`
24+
}
25+
variables := map[string]interface{}{
26+
"license": graphql.String(license),
27+
}
28+
29+
return p.client.Mutate(&mutation, variables)
30+
}
31+
32+
func (c *License) Get() (LicenseData, error) {
33+
var query struct {
34+
License LicenseData
35+
}
36+
variables := map[string]interface{}{}
37+
38+
err := c.client.Query(&query, variables)
39+
40+
return query.License, err
41+
}

cmd/license.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
"os"
20+
21+
"github.com/humio/cli/api"
22+
"github.com/olekukonko/tablewriter"
23+
"github.com/spf13/cobra"
24+
)
25+
26+
// usersCmd represents the users command
27+
func newLicenseCmd() *cobra.Command {
28+
cmd := &cobra.Command{
29+
Use: "license",
30+
Short: "Manage the Humio license [Root Only]",
31+
}
32+
33+
cmd.AddCommand(newLicenseInstallCmd())
34+
cmd.AddCommand(newLicenseShowCmd())
35+
36+
return cmd
37+
}
38+
39+
func printLicenseInfo(license api.LicenseData) {
40+
41+
data := [][]string{
42+
[]string{"Issued At", license.IssuedAt},
43+
[]string{"Expires At", license.ExpiresAt},
44+
}
45+
46+
w := tablewriter.NewWriter(os.Stdout)
47+
w.AppendBulk(data)
48+
w.SetBorder(false)
49+
w.SetColumnSeparator(":")
50+
w.SetColumnAlignment([]int{tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_LEFT})
51+
52+
fmt.Println()
53+
w.Render()
54+
fmt.Println()
55+
}

cmd/license_install.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
}

cmd/license_show.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
"os"
20+
21+
"github.com/spf13/cobra"
22+
)
23+
24+
// usersCmd represents the users command
25+
func newLicenseShowCmd() *cobra.Command {
26+
cmd := &cobra.Command{
27+
Use: "show",
28+
Short: "Show the current Humio license installed",
29+
Args: cobra.ExactArgs(0),
30+
Run: func(cmd *cobra.Command, args []string) {
31+
client := NewApiClient(cmd)
32+
license, apiErr := client.License().Get()
33+
34+
if apiErr != nil {
35+
fmt.Println(fmt.Errorf("error fetching the license: %s", apiErr))
36+
os.Exit(1)
37+
}
38+
39+
printLicenseInfo(license)
40+
},
41+
}
42+
43+
return cmd
44+
}

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ Common Management Commands:
103103
rootCmd.AddCommand(newIngestTokensCmd())
104104
rootCmd.AddCommand(newViewsCmd())
105105
rootCmd.AddCommand(newCompletionCmd())
106+
rootCmd.AddCommand(newLicenseCmd())
106107
}
107108

108109
// initConfig reads in config file and ENV variables if set.

0 commit comments

Comments
 (0)