-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters