From d7e50c3f59ee61a39c3d17cfe166e8a06cb80c21 Mon Sep 17 00:00:00 2001 From: Nitish Tiwari Date: Wed, 2 Aug 2023 18:49:49 +0530 Subject: [PATCH 1/4] File organization --- .gitignore | 5 ++- client.go => cmd/client.go | 4 +-- cmd/pre.go | 30 ++++++++++++++++++ profile_cmds.go => cmd/profile.go | 6 ++-- query_cmds.go => cmd/query.go | 4 +-- stream_cmds.go => cmd/stream.go | 2 +- cli_style.go => cmd/style.go | 2 +- user_cmds.go => cmd/user.go | 2 +- go.mod | 2 +- main.go | 52 +++++++++---------------------- {config => pkg/config}/config.go | 0 {model => pkg/model}/prompt.go | 0 {model => pkg/model}/query.go | 2 +- {model => pkg/model}/status.go | 0 {model => pkg/model}/timerange.go | 0 15 files changed, 61 insertions(+), 50 deletions(-) rename client.go => cmd/client.go (96%) create mode 100644 cmd/pre.go rename profile_cmds.go => cmd/profile.go (94%) rename query_cmds.go => cmd/query.go (95%) rename stream_cmds.go => cmd/stream.go (95%) rename cli_style.go => cmd/style.go (97%) rename user_cmds.go => cmd/user.go (94%) rename {config => pkg/config}/config.go (100%) rename {model => pkg/model}/prompt.go (100%) rename {model => pkg/model}/query.go (99%) rename {model => pkg/model}/status.go (100%) rename {model => pkg/model}/timerange.go (100%) diff --git a/.gitignore b/.gitignore index 32fe832..8ec8f2b 100644 --- a/.gitignore +++ b/.gitignore @@ -21,4 +21,7 @@ go.work .vscode/ -config.toml \ No newline at end of file +config.toml + +# build +pb diff --git a/client.go b/cmd/client.go similarity index 96% rename from client.go rename to cmd/client.go index 50b7b19..bc42825 100644 --- a/client.go +++ b/cmd/client.go @@ -1,10 +1,10 @@ -package main +package cmd import ( - "cli/config" "io" "net/http" "net/url" + "pb/pkg/config" "time" ) diff --git a/cmd/pre.go b/cmd/pre.go new file mode 100644 index 0000000..9d764be --- /dev/null +++ b/cmd/pre.go @@ -0,0 +1,30 @@ +package cmd + +import ( + "errors" + "os" + "pb/pkg/config" + + "github.com/spf13/cobra" +) + +var DefaultProfile config.Profile + +// Check if a profile exists. +// This is required by mostly all commands except profile +func PreRunDefaultProfile(cmd *cobra.Command, args []string) error { + conf, err := config.ReadConfigFromFile() + if err != nil { + if os.IsNotExist(err) { + return errors.New("No config found to run this command. Add a profile using pb profile command") + } else { + return err + } + } + if conf.Profiles == nil || conf.Default_profile == "" { + return errors.New("no profile is configured to run this command. please create one using profile command") + } + + DefaultProfile = conf.Profiles[conf.Default_profile] + return nil +} diff --git a/profile_cmds.go b/cmd/profile.go similarity index 94% rename from profile_cmds.go rename to cmd/profile.go index faf1219..a6b031e 100644 --- a/profile_cmds.go +++ b/cmd/profile.go @@ -1,12 +1,12 @@ -package main +package cmd import ( - "cli/config" - "cli/model" "errors" "fmt" "net/url" "os" + "pb/pkg/config" + "pb/pkg/model" "github.com/charmbracelet/bubbles/table" tea "github.com/charmbracelet/bubbletea" diff --git a/query_cmds.go b/cmd/query.go similarity index 95% rename from query_cmds.go rename to cmd/query.go index 3b077d8..1a353fb 100644 --- a/query_cmds.go +++ b/cmd/query.go @@ -1,9 +1,9 @@ -package main +package cmd import ( - "cli/model" "fmt" "os" + "pb/pkg/model" tea "github.com/charmbracelet/bubbletea" "github.com/spf13/cobra" diff --git a/stream_cmds.go b/cmd/stream.go similarity index 95% rename from stream_cmds.go rename to cmd/stream.go index ccc3434..080de02 100644 --- a/stream_cmds.go +++ b/cmd/stream.go @@ -1,4 +1,4 @@ -package main +package cmd import ( "encoding/json" diff --git a/cli_style.go b/cmd/style.go similarity index 97% rename from cli_style.go rename to cmd/style.go index 95fe0d1..4eb6a3c 100644 --- a/cli_style.go +++ b/cmd/style.go @@ -1,4 +1,4 @@ -package main +package cmd import ( "github.com/charmbracelet/bubbles/table" diff --git a/user_cmds.go b/cmd/user.go similarity index 94% rename from user_cmds.go rename to cmd/user.go index d69eb17..82abb65 100644 --- a/user_cmds.go +++ b/cmd/user.go @@ -1,4 +1,4 @@ -package main +package cmd import ( "encoding/json" diff --git a/go.mod b/go.mod index 5ffa479..6c12180 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module cli +module pb go 1.20 diff --git a/main.go b/main.go index e4f4d5a..00cd72d 100644 --- a/main.go +++ b/main.go @@ -1,34 +1,12 @@ package main import ( - "cli/config" - "errors" "os" + "pb/cmd" "github.com/spf13/cobra" ) -var DefaultProfile config.Profile - -// Check if a profile exists. -// This is required by mostly all commands except profile -func PreRunDefaultProfile(cmd *cobra.Command, args []string) error { - conf, err := config.ReadConfigFromFile() - if err != nil { - if os.IsNotExist(err) { - return errors.New("No config found to run this command. Add a profile using pb profile command") - } else { - return err - } - } - if conf.Profiles == nil || conf.Default_profile == "" { - return errors.New("no profile is configured to run this command. please create one using profile command") - } - - DefaultProfile = conf.Profiles[conf.Default_profile] - return nil -} - // Root command var cli = &cobra.Command{ Use: "pb", @@ -44,32 +22,32 @@ var profile = &cobra.Command{ var user = &cobra.Command{ Use: "user", Short: "Manage users", - PersistentPreRunE: PreRunDefaultProfile, + PersistentPreRunE: cmd.PreRunDefaultProfile, } var stream = &cobra.Command{ Use: "stream", Short: "Manage stream", - PersistentPreRunE: PreRunDefaultProfile, + PersistentPreRunE: cmd.PreRunDefaultProfile, } func main() { - profile.AddCommand(AddProfileCmd) - profile.AddCommand(DeleteProfileCmd) - profile.AddCommand(ListProfileCmd) - profile.AddCommand(DefaultProfileCmd) + profile.AddCommand(cmd.AddProfileCmd) + profile.AddCommand(cmd.DeleteProfileCmd) + profile.AddCommand(cmd.ListProfileCmd) + profile.AddCommand(cmd.DefaultProfileCmd) - user.AddCommand(AddUserCmd) - user.AddCommand(DeleteUserCmd) - user.AddCommand(ListUserCmd) + user.AddCommand(cmd.AddUserCmd) + user.AddCommand(cmd.DeleteUserCmd) + user.AddCommand(cmd.ListUserCmd) - stream.AddCommand(CreateStreamCmd) - stream.AddCommand(ListStreamCmd) - stream.AddCommand(DeleteStreamCmd) - stream.AddCommand(StatStreamCmd) + stream.AddCommand(cmd.CreateStreamCmd) + stream.AddCommand(cmd.ListStreamCmd) + stream.AddCommand(cmd.DeleteStreamCmd) + stream.AddCommand(cmd.StatStreamCmd) cli.AddCommand(profile) - cli.AddCommand(QueryProfileCmd) + cli.AddCommand(cmd.QueryProfileCmd) cli.AddCommand(stream) cli.AddCommand(user) diff --git a/config/config.go b/pkg/config/config.go similarity index 100% rename from config/config.go rename to pkg/config/config.go diff --git a/model/prompt.go b/pkg/model/prompt.go similarity index 100% rename from model/prompt.go rename to pkg/model/prompt.go diff --git a/model/query.go b/pkg/model/query.go similarity index 99% rename from model/query.go rename to pkg/model/query.go index bd759e0..d8ff13e 100644 --- a/model/query.go +++ b/pkg/model/query.go @@ -10,7 +10,7 @@ import ( "os" "time" - "cli/config" + "pb/pkg/config" "github.com/charmbracelet/bubbles/textarea" tea "github.com/charmbracelet/bubbletea" diff --git a/model/status.go b/pkg/model/status.go similarity index 100% rename from model/status.go rename to pkg/model/status.go diff --git a/model/timerange.go b/pkg/model/timerange.go similarity index 100% rename from model/timerange.go rename to pkg/model/timerange.go From 95a2f99f59d429bf516e8f685066de76ec4d41eb Mon Sep 17 00:00:00 2001 From: Nitish Tiwari Date: Wed, 2 Aug 2023 18:52:13 +0530 Subject: [PATCH 2/4] add build and test workflow --- .github/workflows/build.yaml | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 .github/workflows/build.yaml diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 0000000..7274cb7 --- /dev/null +++ b/.github/workflows/build.yaml @@ -0,0 +1,21 @@ +name: Go package + +on: [push] + +jobs: + build: + + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.15' + + - name: Build + run: go build -v ./... + + - name: Test + run: go test -v ./... From 29c002f27b82747cbf1f5ec970875eb54eae13d6 Mon Sep 17 00:00:00 2001 From: Nitish Tiwari Date: Wed, 2 Aug 2023 18:52:52 +0530 Subject: [PATCH 3/4] fix workflow name --- .github/workflows/build.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 7274cb7..449f9b8 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -1,4 +1,4 @@ -name: Go package +name: Go Build and Test on: [push] From 2db7c479a9cb0daaa02e0155c01e952843fb5687 Mon Sep 17 00:00:00 2001 From: Nitish Tiwari Date: Wed, 2 Aug 2023 18:59:48 +0530 Subject: [PATCH 4/4] Add license headers and fix github workflow --- .github/workflows/build.yaml | 2 +- cmd/client.go | 17 +++++++++++++++++ cmd/pre.go | 17 +++++++++++++++++ cmd/profile.go | 17 +++++++++++++++++ cmd/query.go | 17 +++++++++++++++++ cmd/stream.go | 17 +++++++++++++++++ cmd/style.go | 17 +++++++++++++++++ cmd/user.go | 17 +++++++++++++++++ main.go | 17 +++++++++++++++++ pkg/config/config.go | 17 +++++++++++++++++ pkg/model/prompt.go | 17 +++++++++++++++++ pkg/model/query.go | 17 +++++++++++++++++ pkg/model/status.go | 17 +++++++++++++++++ pkg/model/timerange.go | 17 +++++++++++++++++ 14 files changed, 222 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 449f9b8..55aac55 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -12,7 +12,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v4 with: - go-version: '1.15' + go-version: '1.20' - name: Build run: go build -v ./... diff --git a/cmd/client.go b/cmd/client.go index bc42825..10ff572 100644 --- a/cmd/client.go +++ b/cmd/client.go @@ -1,3 +1,20 @@ +// Copyright (c) 2023 Cloudnatively Services Pvt Ltd +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + package cmd import ( diff --git a/cmd/pre.go b/cmd/pre.go index 9d764be..f4c7e9b 100644 --- a/cmd/pre.go +++ b/cmd/pre.go @@ -1,3 +1,20 @@ +// Copyright (c) 2023 Cloudnatively Services Pvt Ltd +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + package cmd import ( diff --git a/cmd/profile.go b/cmd/profile.go index a6b031e..3c43f21 100644 --- a/cmd/profile.go +++ b/cmd/profile.go @@ -1,3 +1,20 @@ +// Copyright (c) 2023 Cloudnatively Services Pvt Ltd +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + package cmd import ( diff --git a/cmd/query.go b/cmd/query.go index 1a353fb..7f1408d 100644 --- a/cmd/query.go +++ b/cmd/query.go @@ -1,3 +1,20 @@ +// Copyright (c) 2023 Cloudnatively Services Pvt Ltd +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + package cmd import ( diff --git a/cmd/stream.go b/cmd/stream.go index 080de02..c497409 100644 --- a/cmd/stream.go +++ b/cmd/stream.go @@ -1,3 +1,20 @@ +// Copyright (c) 2023 Cloudnatively Services Pvt Ltd +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + package cmd import ( diff --git a/cmd/style.go b/cmd/style.go index 4eb6a3c..434577f 100644 --- a/cmd/style.go +++ b/cmd/style.go @@ -1,3 +1,20 @@ +// Copyright (c) 2023 Cloudnatively Services Pvt Ltd +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + package cmd import ( diff --git a/cmd/user.go b/cmd/user.go index 82abb65..283fa22 100644 --- a/cmd/user.go +++ b/cmd/user.go @@ -1,3 +1,20 @@ +// Copyright (c) 2023 Cloudnatively Services Pvt Ltd +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + package cmd import ( diff --git a/main.go b/main.go index 00cd72d..2593f50 100644 --- a/main.go +++ b/main.go @@ -1,3 +1,20 @@ +// Copyright (c) 2023 Cloudnatively Services Pvt Ltd +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + package main import ( diff --git a/pkg/config/config.go b/pkg/config/config.go index 43747bb..2174491 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -1,3 +1,20 @@ +// Copyright (c) 2023 Cloudnatively Services Pvt Ltd +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + package config import ( diff --git a/pkg/model/prompt.go b/pkg/model/prompt.go index 1973f32..cebd052 100644 --- a/pkg/model/prompt.go +++ b/pkg/model/prompt.go @@ -1,3 +1,20 @@ +// Copyright (c) 2023 Cloudnatively Services Pvt Ltd +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + package model import ( diff --git a/pkg/model/query.go b/pkg/model/query.go index d8ff13e..a82bde7 100644 --- a/pkg/model/query.go +++ b/pkg/model/query.go @@ -1,3 +1,20 @@ +// Copyright (c) 2023 Cloudnatively Services Pvt Ltd +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + package model import ( diff --git a/pkg/model/status.go b/pkg/model/status.go index b8c7606..93e5793 100644 --- a/pkg/model/status.go +++ b/pkg/model/status.go @@ -1,3 +1,20 @@ +// Copyright (c) 2023 Cloudnatively Services Pvt Ltd +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + package model import ( diff --git a/pkg/model/timerange.go b/pkg/model/timerange.go index b054263..f7c53a5 100644 --- a/pkg/model/timerange.go +++ b/pkg/model/timerange.go @@ -1,3 +1,20 @@ +// Copyright (c) 2023 Cloudnatively Services Pvt Ltd +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + package model import (