Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement set and show global key #34

Merged
merged 2 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions cmd/appManagementSet.go
Original file line number Diff line number Diff line change
@@ -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")
}
100 changes: 100 additions & 0 deletions cmd/appManagementSetGlobal.go
Original file line number Diff line number Diff line change
@@ -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")
}
93 changes: 93 additions & 0 deletions cmd/appManagementShowGlobal.go
Original file line number Diff line number Diff line change
@@ -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 <Key>",
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")
}