Skip to content

Commit

Permalink
Show more license info
Browse files Browse the repository at this point in the history
  • Loading branch information
anagrius committed Dec 1, 2018
1 parent 758fb41 commit 0458158
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 36 deletions.
87 changes: 78 additions & 9 deletions api/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,57 @@ import (
"github.com/shurcooL/graphql"
)

type License struct {
type Licenses struct {
client *Client
}

type LicenseData struct {
ExpiresAt string
IssuedAt string
type License interface {
ExpiresAt() string
IssuedAt() string
LicenseType() string
}

func (c *Client) License() *License { return &License{client: c} }
type TrialLicense struct {
ExpiresAtVal string
IssuedAtVal string
}

func (l TrialLicense) LicenseType() string {
return "trial"
}

func (l TrialLicense) IssuedAt() string {
return l.IssuedAtVal
}

func (l TrialLicense) ExpiresAt() string {
return l.ExpiresAtVal
}

type OnPremLicense struct {
ID string
ExpiresAtVal string
IssuedAtVal string
IssuedTo string
NumberOfSeats int
Fingerprint string
}

func (l OnPremLicense) IssuedAt() string {
return l.IssuedAtVal
}

func (l OnPremLicense) ExpiresAt() string {
return l.ExpiresAtVal
}

func (l OnPremLicense) LicenseType() string {
return "onprem"
}

func (p *License) Install(license string) error {
func (c *Client) Licenses() *Licenses { return &Licenses{client: c} }

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

var mutation struct {
CreateParser struct {
Expand All @@ -29,13 +68,43 @@ func (p *License) Install(license string) error {
return p.client.Mutate(&mutation, variables)
}

func (c *License) Get() (LicenseData, error) {
func (c *Licenses) Get() (License, error) {
var query struct {
License LicenseData
License struct {
ExpiresAt string
IssuedAt string
OnPrem struct {
ID string `graphql:"uid"`
Owner string
MaxUsers int
Fingerprint string
} `graphql:"... on OnPremLicense"`
}
}
variables := map[string]interface{}{}

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

return query.License, err
if err != nil {
return nil, err
}

var license License
if query.License.OnPrem.ID == "" {
license = TrialLicense{
ExpiresAtVal: query.License.ExpiresAt,
IssuedAtVal: query.License.IssuedAt,
}
} else {
license = OnPremLicense{
ID: query.License.OnPrem.ID,
ExpiresAtVal: query.License.ExpiresAt,
IssuedAtVal: query.License.IssuedAt,
IssuedTo: query.License.OnPrem.Owner,
NumberOfSeats: query.License.OnPrem.MaxUsers,
Fingerprint: query.License.OnPrem.Fingerprint,
}
}

return license, nil
}
23 changes: 15 additions & 8 deletions cmd/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package cmd

import (
"os"
"fmt"

"github.com/humio/cli/api"
"github.com/olekukonko/tablewriter"
Expand All @@ -35,20 +35,27 @@ func newLicenseCmd() *cobra.Command {
return cmd
}

func printLicenseInfo(cmd *cobra.Command, license api.LicenseData) {
func printLicenseInfo(cmd *cobra.Command, license api.License) {

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

data = append(data, []string{"License Type", license.LicenseType()})

if onprem, ok := license.(api.OnPremLicense); ok {
data = append(data, []string{"License ID", onprem.ID})
data = append(data, []string{"Issued To", onprem.IssuedTo})
data = append(data, []string{"Number Of Seats", fmt.Sprintf("%d", onprem.NumberOfSeats)})
data = append(data, []string{"Fingerprint (SHA-1)", onprem.Fingerprint})
}

w := tablewriter.NewWriter(os.Stdout)
data = append(data, []string{"Issued At", license.IssuedAt()})
data = append(data, []string{"Expires At", license.ExpiresAt()})

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

cmd.Println()
w.Render()
cmd.Println()
}
8 changes: 2 additions & 6 deletions cmd/license_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,8 @@ func newLicenseInstallCmd() *cobra.Command {
}

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

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

cmd.Println("License installed")
},
Expand Down
13 changes: 3 additions & 10 deletions cmd/license_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

Expand All @@ -29,14 +26,10 @@ func newLicenseShowCmd() *cobra.Command {
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
client := NewApiClient(cmd)
license, apiErr := client.License().Get()

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

license, apiErr := client.Licenses().Get()
exitOnError(cmd, apiErr, "error fetching the license")
printLicenseInfo(cmd, license)
cmd.Println()
},
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package cmd

import (
"fmt"
"os"
"strings"

"github.com/humio/cli/api"
Expand Down Expand Up @@ -57,7 +56,7 @@ func printUserTable(cmd *cobra.Command, user api.User) {
[]string{"Company", user.Company},
}

w := tablewriter.NewWriter(os.Stdout)
w := tablewriter.NewWriter(cmd.OutOrStdout())
w.AppendBulk(data)
w.SetBorder(false)
w.SetColumnSeparator(":")
Expand Down
2 changes: 1 addition & 1 deletion cmd/views_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func newViewsListCmd() *cobra.Command {
rows[i] = []string{view.Name}
}

w := tablewriter.NewWriter(cmd.OutOrStderr())
w := tablewriter.NewWriter(cmd.OutOrStdout())
w.AppendBulk(rows)
w.SetBorder(false)

Expand Down

0 comments on commit 0458158

Please sign in to comment.