Skip to content

Commit

Permalink
Add user command
Browse files Browse the repository at this point in the history
  • Loading branch information
anagrius committed Nov 20, 2018
1 parent 3a1378e commit 0e054b5
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 77 deletions.
18 changes: 3 additions & 15 deletions command/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package command
import (
"fmt"
"log"
"net/http"
"os"

cli "gopkg.in/urfave/cli.v2"
Expand All @@ -24,32 +23,21 @@ func getServerConfig(c *cli.Context) (server, error) {
return config, nil
}

func newPostRequest(config server, path string) {

req, err := http.NewRequest("POST", config.URL+path, nil)

if err != nil {
log.Fatal(err)
}

req.Header.Add("Authorization", "Bearer "+config.Token)
}

func ensureRepo(server server) {
if server.Repo == "" {
exit("Missing repository argument")
log.Fatal("The command requires the repository to be specified.")
}
}

func ensureURL(server server) {
if server.URL == "" {
exit("Missing url argument")
log.Fatal("You must specify the URL of the Humio server.")
}
}

func ensureToken(server server) {
if server.Token == "" {
exit("Missing API token argument")
exit("API Token not set.")
}
}

Expand Down
13 changes: 13 additions & 0 deletions command/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package command

import (
"bytes"
"context"
"net/http"

"github.com/shurcooL/graphql"
"golang.org/x/oauth2"
)

var client = &http.Client{}
Expand Down Expand Up @@ -39,3 +43,12 @@ func putJSON(url string, jsonStr string, token string) (*http.Response, error) {
}
return client.Do(req)
}

func newGraphQLClient(config server) *graphql.Client {
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: config.Token},
)

httpClient := oauth2.NewClient(context.Background(), src)
return graphql.NewClient(config.URL+"graphql", httpClient)
}
23 changes: 23 additions & 0 deletions command/output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package command

import (
"fmt"

"github.com/ryanuber/columnize"
)

func printTable(rows []string) {

table := columnize.SimpleFormat(rows)

fmt.Println()
fmt.Println(table)
fmt.Println()
}

func yesNo(isTrue bool) string {
if isTrue {
return "yes"
}
return ""
}
13 changes: 13 additions & 0 deletions command/parser.go
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
package command

type testCase struct {
Input string
Output map[string]string
}

type parserConfig struct {
Name string
Description string
Tests []testCase
Example string
Script string
}
30 changes: 3 additions & 27 deletions command/parser_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,10 @@ import (
"strings"

"github.com/shurcooL/graphql"
"golang.org/x/oauth2"
cli "gopkg.in/urfave/cli.v2"
"gopkg.in/yaml.v2"
)

type testCase struct {
Input string
Output map[string]string
}

type parserConfig struct {
Name string
Description string
Tests []testCase
Example string
Script string
}

func ParserAdd(c *cli.Context) error {
config, _ := getServerConfig(c)

Expand All @@ -34,24 +20,14 @@ func ParserAdd(c *cli.Context) error {

filePath := c.Args().First()
file, readErr := ioutil.ReadFile(filePath)

if readErr != nil {
log.Fatalf("Could not read file: %v", filePath)
}
check(readErr)

t := parserConfig{}

err := yaml.Unmarshal(file, &t)
if err != nil {
log.Fatalf("error: %v", err)
}

src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: config.Token},
)
check(err)

httpClient := oauth2.NewClient(context.Background(), src)
client := graphql.NewClient(config.URL+"graphql", httpClient)
client := newGraphQLClient(config)

var m struct {
CreateParser struct {
Expand Down
50 changes: 50 additions & 0 deletions command/parser_get.go
Original file line number Diff line number Diff line change
@@ -1 +1,51 @@
package command

import (
"context"
"io/ioutil"

"github.com/shurcooL/graphql"
"golang.org/x/oauth2"
cli "gopkg.in/urfave/cli.v2"
yaml "gopkg.in/yaml.v2"
)

func ParserGet(c *cli.Context) error {
config, _ := getServerConfig(c)

ensureToken(config)
ensureRepo(config)
ensureURL(config)

parserName := c.Args().First()

src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: config.Token},
)

httpClient := oauth2.NewClient(context.Background(), src)
client := graphql.NewClient(config.URL+"graphql", httpClient)

var q struct {
Repository struct {
Parser struct {
SourceCode string
} `graphql:"parser(name: $parserName)"`
} `graphql:"repository(name: $repositoryName)"`
}

variables := map[string]interface{}{
"parserName": graphql.String(parserName),
"repositoryName": graphql.String(config.Repo),
}

graphqlErr := client.Query(context.Background(), &q, variables)
check(graphqlErr)

d, yamlErr := yaml.Marshal(&q)
check(yamlErr)

ioutil.WriteFile(parserName+".yaml", d, 0644)

return nil
}
22 changes: 3 additions & 19 deletions command/parser_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ package command
import (
"context"
"fmt"
"log"

"github.com/ryanuber/columnize"
"github.com/shurcooL/graphql"
"golang.org/x/oauth2"
cli "gopkg.in/urfave/cli.v2"
)

Expand All @@ -18,13 +15,6 @@ func ParserList(c *cli.Context) error {
ensureRepo(config)
ensureURL(config)

src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: config.Token},
)

httpClient := oauth2.NewClient(context.Background(), src)
client := graphql.NewClient(config.URL+"graphql", httpClient)

var q struct {
Repository struct {
Parsers []struct {
Expand All @@ -38,11 +28,9 @@ func ParserList(c *cli.Context) error {
"repositoryName": graphql.String(config.Repo),
}

client := newGraphQLClient(config)
graphqlErr := client.Query(context.Background(), &q, variables)

if graphqlErr != nil {
log.Fatal(graphqlErr)
}
check(graphqlErr)

var output []string
output = append(output, "Name | Custom")
Expand All @@ -51,11 +39,7 @@ func ParserList(c *cli.Context) error {
output = append(output, fmt.Sprintf("%v | %v", parser.Name, checkmark(!parser.IsBuiltIn)))
}

table := columnize.SimpleFormat(output)

fmt.Println()
fmt.Println(table)
fmt.Println()
printTable(output)

return nil
}
Expand Down
8 changes: 1 addition & 7 deletions command/parser_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"

"github.com/shurcooL/graphql"
"golang.org/x/oauth2"
cli "gopkg.in/urfave/cli.v2"
)

Expand All @@ -18,12 +17,7 @@ func ParserRemove(c *cli.Context) error {

parserName := c.Args().First()

src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: config.Token},
)

httpClient := oauth2.NewClient(context.Background(), src)
client := graphql.NewClient(config.URL+"graphql", httpClient)
client := newGraphQLClient(config)

var m struct {
CreateParser struct {
Expand Down
8 changes: 1 addition & 7 deletions command/users_add_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"

"github.com/shurcooL/graphql"
"golang.org/x/oauth2"
cli "gopkg.in/urfave/cli.v2"
)

Expand All @@ -25,12 +24,7 @@ func updateUser(c *cli.Context, isRoot bool) error {

username := c.Args().First()

src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: config.Token},
)

httpClient := oauth2.NewClient(context.Background(), src)
client := graphql.NewClient(config.URL+"graphql", httpClient)
client := newGraphQLClient(config)

var m struct {
UpdateUser struct {
Expand Down
43 changes: 43 additions & 0 deletions command/users_show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package command

import (
"context"
"strings"

"github.com/shurcooL/graphql"
cli "gopkg.in/urfave/cli.v2"
)

func UsersShow(c *cli.Context) error {
config, _ := getServerConfig(c)

ensureToken(config)
ensureURL(config)

username := c.Args().First()

var q struct {
Account struct {
Username string
FullName string
IsRoot bool
CreatedAt string
} `graphql:"account(username: $username)"`
}

variables := map[string]interface{}{
"username": graphql.String(username),
}

graphqlErr := newGraphQLClient(config).Query(context.Background(), &q, variables)
check(graphqlErr)

userData := []string{q.Account.Username, q.Account.FullName, q.Account.CreatedAt, yesNo(q.Account.IsRoot)}

printTable([]string{
"Username | Name | Created At | Is Root",
strings.Join(userData, "|"),
})

return nil
}
9 changes: 9 additions & 0 deletions command/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package command

import "log"

func check(err error) {
if err != nil {
log.Fatal(err)
}
}
Loading

0 comments on commit 0e054b5

Please sign in to comment.