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

added service operate cmd #231

Merged
merged 3 commits into from
May 16, 2024
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
86 changes: 86 additions & 0 deletions cmd/operate/service.go
Original file line number Diff line number Diff line change
@@ -1 +1,87 @@
package operate

import (
"encoding/json"

"github.com/dream11/odin/internal/service"
fileUtil "github.com/dream11/odin/pkg/util"
serviceProto "github.com/dream11/odin/proto/gen/go/dream11/od/service/v1"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"google.golang.org/protobuf/types/known/structpb"
)

var serviceClient = service.Service{}
var operateServiceCmd = &cobra.Command{
Use: "service",
Short: "operate service",
Args: func(cmd *cobra.Command, args []string) error {
return cobra.NoArgs(cmd, args)
},
Long: `odin operate service [Options]`,
Run: func(cmd *cobra.Command, args []string) {
executeOperateService(cmd)
},
}

func init() {
operateServiceCmd.Flags().StringVar(&name, "name", "", "name of the service")
operateServiceCmd.Flags().StringVar(&env, "env", "", "name of the environment in which the service is deployed")
operateServiceCmd.Flags().StringVar(&operation, "operation", "", "name of the operation to performed on the service")
operateServiceCmd.Flags().StringVar(&options, "options", "{}", "options of the operation in JSON format")
operateServiceCmd.Flags().StringVar(&file, "file", "", "path of the file which contains the options for the operation in JSON format")
if err := operateServiceCmd.MarkFlagRequired("name"); err != nil {
log.Fatal("Error marking 'name' flag as required:", err)
}
if err := operateServiceCmd.MarkFlagRequired("env"); err != nil {
log.Fatal("Error marking 'env' flag as required:", err)
}
if err := operateServiceCmd.MarkFlagRequired("operation"); err != nil {
log.Fatal("Error marking 'operation' flag as required:", err)
}
operateCmd.AddCommand(operateServiceCmd)
}

func executeOperateService(cmd *cobra.Command) {
ctx := cmd.Context()
//validate the variables
var optionsData map[string]interface{}

isOptionsPresent := options != "{}"
isFilePresent := len(file) > 0

if isOptionsPresent && isFilePresent {
log.Fatal("You can provide either --options or --file but not both")
}

if isFilePresent {
parsedConfig, err := fileUtil.ParseFile(file)
if err != nil {
log.Fatal("Error while parsing file " + file + " : " + err.Error())
}
optionsData = parsedConfig.(map[string]interface{})
} else {
err := json.Unmarshal([]byte(options), &optionsData)
if err != nil {
log.Fatal("Unable to parse JSON data " + err.Error())
}
}

config, err := structpb.NewStruct(optionsData)
if err != nil {
log.Fatal("error converting JSON to structpb.Struct: ", err)
}
//call operate service client
err = serviceClient.OperateService(&ctx, &serviceProto.OperateServiceRequest{
EnvName: env,
ServiceName: name,
IsComponentOperation: false,
Operation: operation,
Config: config,
})

if err != nil {
log.Fatal("Failed to operate on service", err)
}

}
38 changes: 38 additions & 0 deletions internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,41 @@ func (e *Service) UndeployService(ctx *context.Context, request *serviceProto.Un
log.Info(message)
return err
}

// OperateService :service operatioms
func (e *Service) OperateService(ctx *context.Context, request *serviceProto.OperateServiceRequest) error {
conn, requestCtx, err := grpcClient(ctx)
if err != nil {
return err
}
client := serviceProto.NewServiceServiceClient(conn)
stream, err := client.OperateService(*requestCtx, request)
if err != nil {
return err
}

log.Info("Starting component operation...")
spinnerInstance := spinner.New(spinner.CharSets[constant.SpinnerType], constant.SpinnerDelay)
err = spinnerInstance.Color(constant.SpinnerColor, constant.SpinnerStyle)
if err != nil {
return err
}
var message string
for {
response, err := stream.Recv()
spinnerInstance.Stop()
if err != nil {
if errors.Is(err, context.Canceled) || err == io.EOF {
break
}
return err
}
if response != nil {
message = response.Message
spinnerInstance.Prefix = fmt.Sprintf(" %s ", response.Message)
spinnerInstance.Start()
}
}
log.Info(message)
return err
}
65 changes: 31 additions & 34 deletions proto/gen/go/dream11/od/component/v1/component.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading