From 95ed1084b41d050b619027637b9150a35cf433e0 Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Wed, 5 Jul 2023 19:01:56 +0800 Subject: [PATCH 1/2] feat: implement set and show key --- cmd/appManagementSet.go | 39 +++++++++++++ cmd/appManagementSetGlobal.go | 100 +++++++++++++++++++++++++++++++++ cmd/appManagementShowGlobal.go | 93 ++++++++++++++++++++++++++++++ main.go | 2 +- 4 files changed, 233 insertions(+), 1 deletion(-) create mode 100644 cmd/appManagementSet.go create mode 100644 cmd/appManagementSetGlobal.go create mode 100644 cmd/appManagementShowGlobal.go diff --git a/cmd/appManagementSet.go b/cmd/appManagementSet.go new file mode 100644 index 0000000..32200f5 --- /dev/null +++ b/cmd/appManagementSet.go @@ -0,0 +1,39 @@ +/* +Copyright © 2023 IceWhaleTech + +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 ( + "github.com/spf13/cobra" +) + +// appManagementSetCmd represents the appManagementSet command +var appManagementSetCmd = &cobra.Command{ + Use: "set", +} + +func init() { + appManagementCmd.AddCommand(appManagementSetCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // appManagementSetCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // appManagementSetCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/appManagementSetGlobal.go b/cmd/appManagementSetGlobal.go new file mode 100644 index 0000000..8a5eedf --- /dev/null +++ b/cmd/appManagementSetGlobal.go @@ -0,0 +1,100 @@ +/* +Copyright © 2023 IceWhaleTech + +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 ( + "context" + "fmt" + "net/http" + "text/tabwriter" + + "github.com/IceWhaleTech/CasaOS-CLI/codegen/app_management" + "github.com/spf13/cobra" +) + +// appManagementSetGlobalCmd represents the appManagementSetGlobal command +var appManagementSetGlobalCmd = &cobra.Command{ + Use: "global", + Short: "set a global variable", + Args: cobra.MatchAll(cobra.ExactArgs(2), func(cmd *cobra.Command, args []string) error { + // the func sould to check the args. + // to force the Key is not a number is hard to do this. + // So I didn't do this. this is a null check. May be you can do this. + return nil + }), + RunE: func(cmd *cobra.Command, args []string) error { + rootURL, err := rootCmd.PersistentFlags().GetString(FlagRootURL) + if err != nil { + return err + } + + url := fmt.Sprintf("http://%s/%s", rootURL, BasePathAppManagement) + + client, err := app_management.NewClientWithResponses(url) + if err != nil { + return err + } + + ctx, cancel := context.WithTimeout(context.Background(), DefaultTimeout) + defer cancel() + + key := args[0] + body := app_management.GlobalSetting{ + Value: args[1], + } + response, err := client.UpdateGlobalSettingWithResponse(ctx, key, body) + if err != nil { + return err + } + + var baseResponse app_management.BaseResponse + + if response.StatusCode() != http.StatusOK { + if err := json.Unmarshal(response.Body, &baseResponse); err != nil { + return fmt.Errorf("%s - %s", response.Status(), response.Body) + } + + return fmt.Errorf("%s - %s", response.Status(), *baseResponse.Message) + } + + w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 3, ' ', 0) + defer w.Flush() + + fmt.Fprintln(w, "Global Key\tGlobal Value") + fmt.Fprintln(w, "--------------\t------------") + + fmt.Fprintf(w, "%s\t%s\t\n", + *response.JSON200.Data.Key, + response.JSON200.Data.Value, + ) + + return nil + }, +} + +func init() { + appManagementSetCmd.AddCommand(appManagementSetGlobalCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // appManagementSetGlobalCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // appManagementSetGlobalCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/appManagementShowGlobal.go b/cmd/appManagementShowGlobal.go new file mode 100644 index 0000000..c3d5d76 --- /dev/null +++ b/cmd/appManagementShowGlobal.go @@ -0,0 +1,93 @@ +/* +Copyright © 2023 IceWhaleTech + +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 ( + "context" + "fmt" + "net/http" + "text/tabwriter" + + "github.com/IceWhaleTech/CasaOS-CLI/codegen/app_management" + "github.com/spf13/cobra" +) + +// appManagementShowGlobalCmd represents the appManagementShowGlobal command +var appManagementShowGlobalCmd = &cobra.Command{ + Use: "global ", + Args: cobra.ExactArgs(0), + RunE: func(cmd *cobra.Command, args []string) error { + rootURL, err := rootCmd.PersistentFlags().GetString(FlagRootURL) + if err != nil { + return err + } + + url := fmt.Sprintf("http://%s/%s", rootURL, BasePathAppManagement) + + client, err := app_management.NewClientWithResponses(url) + if err != nil { + return err + } + + ctx, cancel := context.WithTimeout(context.Background(), DefaultTimeout) + defer cancel() + + response, err := client.GetGlobalSettingsWithResponse(ctx) + if err != nil { + return err + } + + var baseResponse app_management.GetGlobalSettingsResponse + + if response.StatusCode() != http.StatusOK { + if err := json.Unmarshal(response.Body, &baseResponse); err != nil { + return fmt.Errorf("%s - %s", response.Status(), response.Body) + } + + return fmt.Errorf("%s - %s", response.Status(), baseResponse.Body) + } + + w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 3, ' ', 0) + defer w.Flush() + + fmt.Fprintln(w, "Global Key\tGlobal Value") + fmt.Fprintln(w, "--------------\t------------") + + for _, value := range *response.JSON200.Data { + + fmt.Fprintf(w, "%s\t%s\t\n", + *value.Key, + value.Value, + ) + } + + return nil + }, +} + +func init() { + appManagementShowCmd.AddCommand(appManagementShowGlobalCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // appManagementShowGlobalCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // appManagementShowGlobalCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/main.go b/main.go index 50bf02f..4ddb3ae 100644 --- a/main.go +++ b/main.go @@ -1,4 +1,4 @@ -//go:generate bash -c "mkdir -p codegen/app_management && go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen@v1.12.4 -generate types,client -package app_management https://raw.githubusercontent.com/IceWhaleTech/CasaOS-AppManagement/main/api/app_management/openapi.yaml > codegen/app_management/api.go" +//go:generate bash -c "mkdir -p codegen/app_management && go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen@v1.12.4 -generate types,client -package app_management https://raw.githubusercontent.com/CorrectRoadH/CasaOS-AppManagement/feat/open_api_key/api/app_management/openapi.yaml > codegen/app_management/api.go" //go:generate bash -c "mkdir -p codegen/casaos && go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen@v1.12.4 -generate types,client -package casaos https://raw.githubusercontent.com/IceWhaleTech/CasaOS/main/api/casaos/openapi.yaml > codegen/casaos/api.go" //go:generate bash -c "mkdir -p codegen/local_storage && go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen@v1.12.4 -generate types,client -package local_storage https://raw.githubusercontent.com/IceWhaleTech/CasaOS-LocalStorage/main/api/local_storage/openapi.yaml > codegen/local_storage/api.go" //go:generate bash -c "mkdir -p codegen/message_bus && go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen@v1.12.4 -generate types,client -package message_bus https://raw.githubusercontent.com/IceWhaleTech/CasaOS-MessageBus/main/api/message_bus/openapi.yaml > codegen/message_bus/api.go" From 9480d1b009e97a920c69529c4b8fbdf3a9c975a4 Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Thu, 6 Jul 2023 14:40:33 +0800 Subject: [PATCH 2/2] revert openai --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 4ddb3ae..50bf02f 100644 --- a/main.go +++ b/main.go @@ -1,4 +1,4 @@ -//go:generate bash -c "mkdir -p codegen/app_management && go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen@v1.12.4 -generate types,client -package app_management https://raw.githubusercontent.com/CorrectRoadH/CasaOS-AppManagement/feat/open_api_key/api/app_management/openapi.yaml > codegen/app_management/api.go" +//go:generate bash -c "mkdir -p codegen/app_management && go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen@v1.12.4 -generate types,client -package app_management https://raw.githubusercontent.com/IceWhaleTech/CasaOS-AppManagement/main/api/app_management/openapi.yaml > codegen/app_management/api.go" //go:generate bash -c "mkdir -p codegen/casaos && go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen@v1.12.4 -generate types,client -package casaos https://raw.githubusercontent.com/IceWhaleTech/CasaOS/main/api/casaos/openapi.yaml > codegen/casaos/api.go" //go:generate bash -c "mkdir -p codegen/local_storage && go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen@v1.12.4 -generate types,client -package local_storage https://raw.githubusercontent.com/IceWhaleTech/CasaOS-LocalStorage/main/api/local_storage/openapi.yaml > codegen/local_storage/api.go" //go:generate bash -c "mkdir -p codegen/message_bus && go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen@v1.12.4 -generate types,client -package message_bus https://raw.githubusercontent.com/IceWhaleTech/CasaOS-MessageBus/main/api/message_bus/openapi.yaml > codegen/message_bus/api.go"