Skip to content

Commit

Permalink
Add license command
Browse files Browse the repository at this point in the history
  • Loading branch information
anagrius committed Nov 29, 2018
1 parent b9072fc commit b3bf727
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 0 deletions.
41 changes: 41 additions & 0 deletions api/license.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package api

import (
"github.com/shurcooL/graphql"
)

type License struct {
client *Client
}

type LicenseData struct {
ExpiresAt string
IssuedAt string
}

func (c *Client) License() *License { return &License{client: c} }

func (p *License) Install(license string) error {

var mutation struct {
CreateParser struct {
Type string `graphql:"__typename"`
} `graphql:"updateLicenseKey(license: $license)"`
}
variables := map[string]interface{}{
"license": graphql.String(license),
}

return p.client.Mutate(&mutation, variables)
}

func (c *License) Get() (LicenseData, error) {
var query struct {
License LicenseData
}
variables := map[string]interface{}{}

err := c.client.Query(&query, variables)

return query.License, err
}
55 changes: 55 additions & 0 deletions cmd/license.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright © 2018 Humio Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"
"os"

"github.com/humio/cli/api"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)

// usersCmd represents the users command
func newLicenseCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "license",
Short: "Manage the Humio license [Root Only]",
}

cmd.AddCommand(newLicenseInstallCmd())
cmd.AddCommand(newLicenseShowCmd())

return cmd
}

func printLicenseInfo(license api.LicenseData) {

data := [][]string{
[]string{"Issued At", license.IssuedAt},
[]string{"Expires At", license.ExpiresAt},
}

w := tablewriter.NewWriter(os.Stdout)
w.AppendBulk(data)
w.SetBorder(false)
w.SetColumnSeparator(":")
w.SetColumnAlignment([]int{tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_LEFT})

fmt.Println()
w.Render()
fmt.Println()
}
66 changes: 66 additions & 0 deletions cmd/license_install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright © 2018 Humio Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"
"io/ioutil"
"os"

"github.com/spf13/cobra"
)

// usersCmd represents the users command
func newLicenseInstallCmd() *cobra.Command {
var license string

cmd := &cobra.Command{
Use: "install [flags] (<license-file> | --license=<string>)",
Short: "Install a Humio license",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 1 {
filepath := args[0]

licenseBytes, readErr := ioutil.ReadFile(filepath)
if readErr != nil {
fmt.Println(fmt.Errorf("error reading license file: %s", readErr))
os.Exit(1)
}

license = string(licenseBytes)
} else if license != "" {
// License set from flag
} else {
fmt.Println("Expected either an argument <filename> or flag --license=<license>.")
cmd.Help()
os.Exit(1)
}

client := NewApiClient(cmd)
installErr := client.License().Install(license)

if installErr != nil {
fmt.Println(fmt.Errorf("error installing license: %s", installErr))
os.Exit(1)
}

fmt.Println("License installed")
},
}

cmd.Flags().StringVarP(&license, "license", "l", "", "A string with the content license license file.")

return cmd
}
44 changes: 44 additions & 0 deletions cmd/license_show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright © 2018 Humio Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

// usersCmd represents the users command
func newLicenseShowCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "show",
Short: "Show the current Humio license installed",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
client := NewApiClient(cmd)
license, apiErr := client.License().Get()

if apiErr != nil {
fmt.Println(fmt.Errorf("error fetching the license: %s", apiErr))
os.Exit(1)
}

printLicenseInfo(license)
},
}

return cmd
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Common Management Commands:
rootCmd.AddCommand(newIngestTokensCmd())
rootCmd.AddCommand(newViewsCmd())
rootCmd.AddCommand(newCompletionCmd())
rootCmd.AddCommand(newLicenseCmd())
}

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

0 comments on commit b3bf727

Please sign in to comment.