From a1567f48752774c050a26cebf6df1d7b0e2f03c2 Mon Sep 17 00:00:00 2001 From: hirosassa Date: Sat, 26 Jun 2021 11:03:03 +0900 Subject: [PATCH 1/4] add google workspace commands --- .config/default.toml | 3 + README.md | 32 +++ alias/alias.go | 23 +- cli/googleworkspace.go | 234 +++++++++++++++++++ cli/root.go | 1 + providers/googleworkspace/googleworkspace.go | 166 +++++++++++++ 6 files changed, 448 insertions(+), 11 deletions(-) create mode 100644 cli/googleworkspace.go create mode 100644 providers/googleworkspace/googleworkspace.go diff --git a/.config/default.toml b/.config/default.toml index 7cb8e56..f69283e 100644 --- a/.config/default.toml +++ b/.config/default.toml @@ -13,6 +13,9 @@ [google] [google.alias] +[googleworkspace] + [googleworkspace.alias] + [youtube] [youtube.alias] diff --git a/README.md b/README.md index 013ca43..2b472f2 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ - [GCP](#gcp) - [Github](#github) - [Google](#google) + - [GoogleWorkspace](#googleworkspace) - [Pagerduty](#pagerduty) - [Youtube](#youtube) - [(Advanced): Docker image](#advanced-docker-image) @@ -374,6 +375,37 @@ $ biko g [product] [flag(s)] | Search | Search on Google | `search`, `s` | `--query, -q` | +If you bump into something you want to lookup when you are using the terminal... + +``` +$ biko g s -q "How to configure biko" +``` + +Blazing fast. + +### Google Workspace + +* Open Google Workspace search from your terminal. + +``` +$ biko googleworkspace [product] [flag(s)] +# or +$ biko gw [product] [flag(s)] +``` + +| Product | What | Command | Flags(Optional) | +| :----: |:----:|:----:|:----:| +| drive | Search on Google Drive | `drive`, `dr` | `--query, -q` | +| document | Search on Google Docs | `document`, `dc` | `--query, -q` | +| document | Create a new Google Docs | `document new`, `dc n` | - | +| spreadsheets | Search on Google Sheets | `spreadsheets`, `ss` | `--query, -q` | +| spreadsheets | Create a new Google Sheets | `spreadsheets new`, `ss n` | - | +| presentation | Search on Google Slides | `presentation`, `pr` | `--query, -q` | +| presentation | Create a new Google Slides | `presentation new`, `pr new` | - | +| forms | Search on Google Forms | `forms`, `fm` | `--query, -q` | +| forms | Create a new Google Forms | `forms new`, `fm n` | - | + + If you bump into something you want to lookup when you are using the terminal... ``` diff --git a/alias/alias.go b/alias/alias.go index 9edd1a7..02a02e2 100644 --- a/alias/alias.go +++ b/alias/alias.go @@ -27,17 +27,18 @@ var ( // TomlConfig ... type TomlConfig struct { - AWS map[string]interface{} `toml:"aws"` - Azure map[string]interface{} `toml:"azure"` - GCP map[string]interface{} `toml:"gcp"` - Datadog map[string]interface{} `toml:"datadog"` - Google map[string]interface{} `toml:"google"` - Youtube map[string]interface{} `toml:"youtube"` - PagerDuty map[string]interface{} `toml:"pagerduty"` - Github map[string]interface{} `toml:"github"` - CircleCI map[string]interface{} `toml:"circleci"` - Firebase map[string]interface{} `toml:"firebase"` - JIRA map[string]interface{} `toml:"jira"` + AWS map[string]interface{} `toml:"aws"` + Azure map[string]interface{} `toml:"azure"` + GCP map[string]interface{} `toml:"gcp"` + Datadog map[string]interface{} `toml:"datadog"` + Google map[string]interface{} `toml:"google"` + GoogleWorkspace map[string]interface{} `toml:"googleworkspace"` + Youtube map[string]interface{} `toml:"youtube"` + PagerDuty map[string]interface{} `toml:"pagerduty"` + Github map[string]interface{} `toml:"github"` + CircleCI map[string]interface{} `toml:"circleci"` + Firebase map[string]interface{} `toml:"firebase"` + JIRA map[string]interface{} `toml:"jira"` } // GetConfig ... diff --git a/cli/googleworkspace.go b/cli/googleworkspace.go new file mode 100644 index 0000000..7086e40 --- /dev/null +++ b/cli/googleworkspace.go @@ -0,0 +1,234 @@ +// Copyright 2019 The Biko Authors. +// +// 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 cli + +import ( + "github.com/KeisukeYamashita/biko/browser" + "github.com/KeisukeYamashita/biko/providers/googleworkspace" + "github.com/urfave/cli" +) + +func newGoogleWorkspaceCmd() cli.Command { + return cli.Command{ + Name: "googleworkspace", + Aliases: []string{"gw"}, + Usage: "Open Google Workspace resource", + Category: categoryWebService, + Flags: []cli.Flag{}, + Action: func(c *cli.Context) error { + g, err := googleworkspace.GetProvider() + if err != nil { + return err + } + return browser.Open(c, g) + }, + Subcommands: []cli.Command{ + newDriveCmd(), + newDocumentCmd(), + newSpreadsheetCmd(), + newPresentationCmd(), + }, + } +} + +func newDriveCmd() cli.Command { + return cli.Command{ + Name: "drive", + Aliases: []string{"dr"}, + Usage: "Open Google Drive directory", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "query, q", + Usage: "Query to search", + }, + }, + Action: func(c *cli.Context) error { + g, err := googleworkspace.GetProvider() + if err != nil { + return err + } + return browser.Open(c, g) + }, + } + +} + +func newDocumentCmd() cli.Command { + return cli.Command{ + Name: "document", + Aliases: []string{"dc"}, + Usage: "Open Google Document page", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "query, q", + Usage: "Query a page", + }, + }, + Action: func(c *cli.Context) error { + g, err := googleworkspace.GetProvider() + if err != nil { + return err + } + return browser.Open(c, g) + }, + Subcommands: []cli.Command{ + newNewDocumentCmd(), + }, + } + +} + +func newSpreadsheetCmd() cli.Command { + return cli.Command{ + Name: "spreadsheets", + Aliases: []string{"ss"}, + Usage: "Open Google Spreadsheets page", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "query, q", + Usage: "Query a page", + }, + }, + Action: func(c *cli.Context) error { + g, err := googleworkspace.GetProvider() + if err != nil { + return err + } + return browser.Open(c, g) + }, + Subcommands: []cli.Command{ + newNewSpreadsheetCmd(), + }, + } + +} + +func newPresentationCmd() cli.Command { + return cli.Command{ + Name: "presentation", + Aliases: []string{"pr"}, + Usage: "Open Google Slides page", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "query, q", + Usage: "Query a page", + }, + }, + Action: func(c *cli.Context) error { + g, err := googleworkspace.GetProvider() + if err != nil { + return err + } + return browser.Open(c, g) + }, + Subcommands: []cli.Command{ + newNewPresentationCmd(), + }, + } + +} + +func newFormsCmd() cli.Command { + return cli.Command{ + Name: "forms", + Aliases: []string{"fm"}, + Usage: "Open Google Forms page", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "query, q", + Usage: "Query a page", + }, + }, + Action: func(c *cli.Context) error { + g, err := googleworkspace.GetProvider() + if err != nil { + return err + } + return browser.Open(c, g) + }, + Subcommands: []cli.Command{ + newNewFormsCmd(), + }, + } + +} + +func newNewDocumentCmd() cli.Command { + return cli.Command{ + Name: "new", + Aliases: []string{"n"}, + Usage: "Create a new document", + Flags: []cli.Flag{}, + Action: func(c *cli.Context) error { + g, err := googleworkspace.GetProvider() + if err != nil { + return err + } + return browser.Open(c, g) + }, + } + +} + +func newNewSpreadsheetCmd() cli.Command { + return cli.Command{ + Name: "new", + Aliases: []string{"n"}, + Usage: "Create a new spreadsheet", + Flags: []cli.Flag{}, + Action: func(c *cli.Context) error { + g, err := googleworkspace.GetProvider() + if err != nil { + return err + } + return browser.Open(c, g) + }, + } + +} + +func newNewPresentationCmd() cli.Command { + return cli.Command{ + Name: "new", + Aliases: []string{"n"}, + Usage: "Create a new presentation", + Flags: []cli.Flag{}, + Action: func(c *cli.Context) error { + g, err := googleworkspace.GetProvider() + if err != nil { + return err + } + return browser.Open(c, g) + }, + } + +} + +func newNewFormsCmd() cli.Command { + return cli.Command{ + Name: "new", + Aliases: []string{"n"}, + Usage: "Create a new form", + Flags: []cli.Flag{}, + Action: func(c *cli.Context) error { + g, err := googleworkspace.GetProvider() + if err != nil { + return err + } + return browser.Open(c, g) + }, + } + +} diff --git a/cli/root.go b/cli/root.go index a99f719..4827ffd 100644 --- a/cli/root.go +++ b/cli/root.go @@ -44,6 +44,7 @@ func rootSubCommands() []cli.Command { newGCPCmd(), newGithubCmd(), newGoogleCmd(), + newGoogleWorkspaceCmd(), newJIRACmd(), newPagerDutyCmd(), newVersionCmd(), diff --git a/providers/googleworkspace/googleworkspace.go b/providers/googleworkspace/googleworkspace.go new file mode 100644 index 0000000..537b928 --- /dev/null +++ b/providers/googleworkspace/googleworkspace.go @@ -0,0 +1,166 @@ +// Copyright 2019 The Biko Authors. +// +// 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 googleworkspace + +import ( + "net/url" + "path" + "strings" + + "github.com/KeisukeYamashita/biko/alias" + "github.com/urfave/cli" +) + +const ( + drive = "drive" + document = "document" + spreadsheets = "spreadsheets" + presentation = "presentation" + forms = "forms" +) + +// Provider ... +type Provider struct { + baseURL *url.URL + URL *url.URL + Ctx *cli.Context + Aliases map[string]interface{} +} + +// GetProvider ... +func GetProvider() (*Provider, error) { + conf, err := alias.GetConfig() + if err != nil { + return nil, err + } + + return &Provider{ + Aliases: conf.GoogleWorkspace["alias"].(map[string]interface{}), + }, nil +} + +// Init ... +func (p *Provider) Init(c *cli.Context) error { + p.Ctx = c + return nil +} + +// GetTargetURL ... +func (p *Provider) GetTargetURL() (string, error) { + product := p.Ctx.Command.Name + if product == "new" { + return p.getNewCmdURL(), nil + } + + var baseURL string + switch product { + case drive: + baseURL = "https://drive.google.com" + case document, spreadsheets, presentation, forms: + baseURL = "https://docs.google.com/" + } + + var err error + if p.baseURL, err = url.Parse(baseURL); err != nil { + return "", err + } + p.addProductPath(product) + return p.URL.String(), nil +} + +func (p *Provider) addProductPath(product string) { + p.URL = p.baseURL + switch product { + case drive: + p.join(drive) + param := url.Values{} + var query string + if query = p.GetCtxString("query"); query != "" { + p.join("search") + param.Add("q", query) + p.URL.RawQuery = param.Encode() + } + case document: + p.join(document) + param := url.Values{} + var query string + if query = p.GetCtxString("query"); query != "" { + param.Add("q", query) + p.URL.RawQuery = param.Encode() + } + case spreadsheets: + p.join(spreadsheets) + param := url.Values{} + var query string + if query = p.GetCtxString("query"); query != "" { + param.Add("q", query) + p.URL.RawQuery = param.Encode() + } + case presentation: + p.join(presentation) + param := url.Values{} + var query string + if query = p.GetCtxString("query"); query != "" { + param.Add("q", query) + p.URL.RawQuery = param.Encode() + } + case forms: + p.join(forms) + param := url.Values{} + var query string + if query = p.GetCtxString("query"); query != "" { + param.Add("q", query) + p.URL.RawQuery = param.Encode() + } + } +} + +func (p *Provider) join(additionPath string) { + if p.URL == nil { + p.URL = p.baseURL + } + p.URL.Path = path.Join(p.URL.Path, additionPath) +} + +func (p *Provider) getNewCmdURL() string { + parentCmd := strings.Split(p.Ctx.Command.FullName(), " ")[0] + switch parentCmd { + case document: + return "https://document.new" + case spreadsheets: + return "https://spreadsheets.new" + case presentation: + return "https://presentation.new" + case forms: + return "https://forms.new" + } + return "" +} + +// GetCtxString ... +func (p *Provider) GetCtxString(str string) string { + key := p.Ctx.String(str) + if key == "" { + return "" + } + value, ok := p.Aliases[key].(string) + if !ok { + return key + } + if value == "" { + return key + } + return value +} From c2de7c349b2e63c35ffa319a822608bb8dcddc01 Mon Sep 17 00:00:00 2001 From: hirosassa Date: Sat, 26 Jun 2021 11:05:24 +0900 Subject: [PATCH 2/4] remove mistakes --- README.md | 8 -------- 1 file changed, 8 deletions(-) diff --git a/README.md b/README.md index 2b472f2..4a50789 100644 --- a/README.md +++ b/README.md @@ -406,14 +406,6 @@ $ biko gw [product] [flag(s)] | forms | Create a new Google Forms | `forms new`, `fm n` | - | -If you bump into something you want to lookup when you are using the terminal... - -``` -$ biko g s -q "How to configure biko" -``` - -Blazing fast. - ### Pagerduty * If you are using SSO, you need to pass `--org` or configure `BIKO_PAGERDUTY` From 31c2e72c8ec4a1eb1eda4b0b1b452b7fadcc4f3a Mon Sep 17 00:00:00 2001 From: hirosassa Date: Sun, 27 Jun 2021 12:58:22 +0900 Subject: [PATCH 3/4] use flag for create new docs --- README.md | 12 +-- cli/googleworkspace.go | 101 ++++--------------- providers/googleworkspace/googleworkspace.go | 10 +- 3 files changed, 30 insertions(+), 93 deletions(-) diff --git a/README.md b/README.md index 4a50789..a09c9e7 100644 --- a/README.md +++ b/README.md @@ -393,17 +393,17 @@ $ biko googleworkspace [product] [flag(s)] $ biko gw [product] [flag(s)] ``` -| Product | What | Command | Flags(Optional) | -| :----: |:----:|:----:|:----:| +| Product | What | Command | Flags(Optional) | +| :----: | :----: | :----: | :----: | | drive | Search on Google Drive | `drive`, `dr` | `--query, -q` | | document | Search on Google Docs | `document`, `dc` | `--query, -q` | -| document | Create a new Google Docs | `document new`, `dc n` | - | +| document | Create a new Google Docs | `document`, `dc` | `--new, -n` | | spreadsheets | Search on Google Sheets | `spreadsheets`, `ss` | `--query, -q` | -| spreadsheets | Create a new Google Sheets | `spreadsheets new`, `ss n` | - | +| spreadsheets | Create a new Google Sheets | `spreadsheets`, `ss` | `--new, -n` | | presentation | Search on Google Slides | `presentation`, `pr` | `--query, -q` | -| presentation | Create a new Google Slides | `presentation new`, `pr new` | - | +| presentation | Create a new Google Slides | `presentation`, `pr` | `--new, -n` | | forms | Search on Google Forms | `forms`, `fm` | `--query, -q` | -| forms | Create a new Google Forms | `forms new`, `fm n` | - | +| forms | Create a new Google Forms | `forms`, `fm` | `--new, -n` | ### Pagerduty diff --git a/cli/googleworkspace.go b/cli/googleworkspace.go index 7086e40..4cc55f1 100644 --- a/cli/googleworkspace.go +++ b/cli/googleworkspace.go @@ -37,8 +37,9 @@ func newGoogleWorkspaceCmd() cli.Command { Subcommands: []cli.Command{ newDriveCmd(), newDocumentCmd(), - newSpreadsheetCmd(), + newSpreadsheetsCmd(), newPresentationCmd(), + newFormsCmd(), }, } } @@ -75,6 +76,10 @@ func newDocumentCmd() cli.Command { Name: "query, q", Usage: "Query a page", }, + cli.BoolFlag{ + Name: "new, n", + Usage: "Create a new document (this flag prioritize over query flag)", + }, }, Action: func(c *cli.Context) error { g, err := googleworkspace.GetProvider() @@ -83,14 +88,11 @@ func newDocumentCmd() cli.Command { } return browser.Open(c, g) }, - Subcommands: []cli.Command{ - newNewDocumentCmd(), - }, } } -func newSpreadsheetCmd() cli.Command { +func newSpreadsheetsCmd() cli.Command { return cli.Command{ Name: "spreadsheets", Aliases: []string{"ss"}, @@ -100,6 +102,10 @@ func newSpreadsheetCmd() cli.Command { Name: "query, q", Usage: "Query a page", }, + cli.BoolFlag{ + Name: "new, n", + Usage: "Create a new spreadsheet (this flag prioritize over query flag)", + }, }, Action: func(c *cli.Context) error { g, err := googleworkspace.GetProvider() @@ -108,9 +114,6 @@ func newSpreadsheetCmd() cli.Command { } return browser.Open(c, g) }, - Subcommands: []cli.Command{ - newNewSpreadsheetCmd(), - }, } } @@ -125,6 +128,10 @@ func newPresentationCmd() cli.Command { Name: "query, q", Usage: "Query a page", }, + cli.BoolFlag{ + Name: "new, n", + Usage: "Create a new presentation (this flag prioritize over query flag)", + }, }, Action: func(c *cli.Context) error { g, err := googleworkspace.GetProvider() @@ -133,9 +140,6 @@ func newPresentationCmd() cli.Command { } return browser.Open(c, g) }, - Subcommands: []cli.Command{ - newNewPresentationCmd(), - }, } } @@ -150,6 +154,10 @@ func newFormsCmd() cli.Command { Name: "query, q", Usage: "Query a page", }, + cli.BoolFlag{ + Name: "new, n", + Usage: "Create a new form (this flag prioritize over query flag)", + }, }, Action: func(c *cli.Context) error { g, err := googleworkspace.GetProvider() @@ -158,77 +166,6 @@ func newFormsCmd() cli.Command { } return browser.Open(c, g) }, - Subcommands: []cli.Command{ - newNewFormsCmd(), - }, - } - -} - -func newNewDocumentCmd() cli.Command { - return cli.Command{ - Name: "new", - Aliases: []string{"n"}, - Usage: "Create a new document", - Flags: []cli.Flag{}, - Action: func(c *cli.Context) error { - g, err := googleworkspace.GetProvider() - if err != nil { - return err - } - return browser.Open(c, g) - }, - } - -} - -func newNewSpreadsheetCmd() cli.Command { - return cli.Command{ - Name: "new", - Aliases: []string{"n"}, - Usage: "Create a new spreadsheet", - Flags: []cli.Flag{}, - Action: func(c *cli.Context) error { - g, err := googleworkspace.GetProvider() - if err != nil { - return err - } - return browser.Open(c, g) - }, - } - -} - -func newNewPresentationCmd() cli.Command { - return cli.Command{ - Name: "new", - Aliases: []string{"n"}, - Usage: "Create a new presentation", - Flags: []cli.Flag{}, - Action: func(c *cli.Context) error { - g, err := googleworkspace.GetProvider() - if err != nil { - return err - } - return browser.Open(c, g) - }, - } - -} - -func newNewFormsCmd() cli.Command { - return cli.Command{ - Name: "new", - Aliases: []string{"n"}, - Usage: "Create a new form", - Flags: []cli.Flag{}, - Action: func(c *cli.Context) error { - g, err := googleworkspace.GetProvider() - if err != nil { - return err - } - return browser.Open(c, g) - }, } } diff --git a/providers/googleworkspace/googleworkspace.go b/providers/googleworkspace/googleworkspace.go index 537b928..1567c5f 100644 --- a/providers/googleworkspace/googleworkspace.go +++ b/providers/googleworkspace/googleworkspace.go @@ -17,7 +17,6 @@ package googleworkspace import ( "net/url" "path" - "strings" "github.com/KeisukeYamashita/biko/alias" "github.com/urfave/cli" @@ -59,12 +58,13 @@ func (p *Provider) Init(c *cli.Context) error { // GetTargetURL ... func (p *Provider) GetTargetURL() (string, error) { - product := p.Ctx.Command.Name - if product == "new" { + newFlag := p.GetCtxString("new") + if newFlag == "true" { // HACK: need to fix GetCtxString return p.getNewCmdURL(), nil } var baseURL string + product := p.Ctx.Command.Name switch product { case drive: baseURL = "https://drive.google.com" @@ -135,8 +135,8 @@ func (p *Provider) join(additionPath string) { } func (p *Provider) getNewCmdURL() string { - parentCmd := strings.Split(p.Ctx.Command.FullName(), " ")[0] - switch parentCmd { + product := p.Ctx.Command.Name + switch product { case document: return "https://document.new" case spreadsheets: From e42cd964f1b8bf8e713e281e78e68cd559d86ea3 Mon Sep 17 00:00:00 2001 From: hirosassa Date: Sun, 27 Jun 2021 20:12:00 +0900 Subject: [PATCH 4/4] review comment: dc -> doc --- README.md | 4 ++-- cli/googleworkspace.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a09c9e7..f653cbf 100644 --- a/README.md +++ b/README.md @@ -396,8 +396,8 @@ $ biko gw [product] [flag(s)] | Product | What | Command | Flags(Optional) | | :----: | :----: | :----: | :----: | | drive | Search on Google Drive | `drive`, `dr` | `--query, -q` | -| document | Search on Google Docs | `document`, `dc` | `--query, -q` | -| document | Create a new Google Docs | `document`, `dc` | `--new, -n` | +| document | Search on Google Docs | `document`, `doc` | `--query, -q` | +| document | Create a new Google Docs | `document`, `doc` | `--new, -n` | | spreadsheets | Search on Google Sheets | `spreadsheets`, `ss` | `--query, -q` | | spreadsheets | Create a new Google Sheets | `spreadsheets`, `ss` | `--new, -n` | | presentation | Search on Google Slides | `presentation`, `pr` | `--query, -q` | diff --git a/cli/googleworkspace.go b/cli/googleworkspace.go index 4cc55f1..62973d2 100644 --- a/cli/googleworkspace.go +++ b/cli/googleworkspace.go @@ -69,7 +69,7 @@ func newDriveCmd() cli.Command { func newDocumentCmd() cli.Command { return cli.Command{ Name: "document", - Aliases: []string{"dc"}, + Aliases: []string{"doc"}, Usage: "Open Google Document page", Flags: []cli.Flag{ cli.StringFlag{