Skip to content

Commit

Permalink
[ioctl]: update ws project format (#4224)
Browse files Browse the repository at this point in the history
* feat(project): update project format

* feat(ioctl): add projectConfig struct in ws
  • Loading branch information
hunshenshi authored Apr 11, 2024
1 parent 1791f82 commit 9496ed5
Showing 1 changed file with 44 additions and 11 deletions.
55 changes: 44 additions & 11 deletions ioctl/cmd/ws/wsprojectconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ var (
Use: "config",
Short: config.TranslateInLang(wsProjectConfigShorts, config.UILanguage),
RunE: func(cmd *cobra.Command, args []string) error {
dataSource, err := cmd.Flags().GetString("data-source")
if err != nil {
return errors.Wrap(err, "failed to get flag data-source")
}
defaultVersion, err := cmd.Flags().GetString("default-version")
if err != nil {
return errors.Wrap(err, "failed to get flag default-version")
}
version, err := cmd.Flags().GetString("version")
if err != nil {
return errors.Wrap(err, "failed to get flag version")
Expand All @@ -47,7 +55,7 @@ var (
return errors.Wrap(err, "failed to get flag output-file")
}

out, err := generateProjectFile(version, vmType, codeFile, confFile, expParam, outputFile)
out, err := generateProjectFile(dataSource, defaultVersion, version, vmType, codeFile, confFile, expParam, outputFile)
if err != nil {
return output.PrintError(err)
}
Expand All @@ -62,6 +70,14 @@ var (
config.Chinese: "生成项目的配置文件",
}

_flagDataSourceUsages = map[config.Language]string{
config.English: "data source of the project",
config.Chinese: "该project的数据源",
}
_flagDefaultVersionUsages = map[config.Language]string{
config.English: "default version for the project config",
config.Chinese: "该project config的默认版本号",
}
_flagVersionUsages = map[config.Language]string{
config.English: "version for the project config",
config.Chinese: "该project config的版本号",
Expand Down Expand Up @@ -89,19 +105,29 @@ var (
)

func init() {
wsProjectConfig.Flags().StringP("data-source", "s", "", config.TranslateInLang(_flagDataSourceUsages, config.UILanguage))
wsProjectConfig.Flags().StringP("default-version", "d", "0.1", config.TranslateInLang(_flagDefaultVersionUsages, config.UILanguage))
wsProjectConfig.Flags().StringP("version", "v", "", config.TranslateInLang(_flagVersionUsages, config.UILanguage))
wsProjectConfig.Flags().StringP("vm-type", "t", "", config.TranslateInLang(_flagVMTypeUsages, config.UILanguage))
wsProjectConfig.Flags().StringP("code-file", "i", "", config.TranslateInLang(_flagCodeFileUsages, config.UILanguage))
wsProjectConfig.Flags().StringP("conf-file", "c", "", config.TranslateInLang(_flagConfFileUsages, config.UILanguage))
wsProjectConfig.Flags().StringP("expand-param", "e", "", config.TranslateInLang(_flagExpandParamUsages, config.UILanguage))
wsProjectConfig.Flags().StringP("output-file", "u", "", config.TranslateInLang(_flagOutputFileUsages, config.UILanguage))

_ = wsProjectConfig.MarkFlagRequired("version")
_ = wsProjectConfig.MarkFlagRequired("data-source")
_ = wsProjectConfig.MarkFlagRequired("vm-type")
_ = wsProjectConfig.MarkFlagRequired("code-file")
}

func generateProjectFile(version, vmType, codeFile, confFile, expParam, outputFile string) (string, error) {
type projectConfig struct {
Version string `json:"version"`
VMType string `json:"vmType"`
Output interface{} `json:"output"`
CodeExpParam string `json:"codeExpParam,omitempty"`
Code string `json:"code"`
}

func generateProjectFile(dataSource, defaultVersion, version, vmType, codeFile, confFile, expParam, outputFile string) (string, error) {
tye, err := stringToVMType(vmType)
if err != nil {
return "", err
Expand All @@ -113,17 +139,21 @@ func generateProjectFile(version, vmType, codeFile, confFile, expParam, outputFi
}

var (
confMaps = make([]map[string]interface{}, 0)
confMap = make(map[string]interface{})
verMap projectConfig
verMaps = make([]projectConfig, 0)
outputMap = make(map[string]interface{})
)

if expParam != "" {
confMap["codeExpParam"] = expParam
verMap.CodeExpParam = expParam
}
verMap.VMType = string(tye)
verMap.Code = hexString
if version == "" {
version = "0.1"
}
confMap["vmType"] = string(tye)
confMap["code"] = hexString
confMap["version"] = version
verMap.Version = version

output := []byte(`{
"type": "stdout"
Expand All @@ -137,10 +167,13 @@ func generateProjectFile(version, vmType, codeFile, confFile, expParam, outputFi
if err := json.Unmarshal(output, &outputMap); err != nil {
return "", errors.Wrap(err, "failed to unmarshal output file")
}
confMap["output"] = outputMap
verMap.Output = outputMap
verMaps = append(verMaps, verMap)

confMaps = append(confMaps, confMap)
jsonConf, err := json.MarshalIndent(confMaps, "", " ")
confMap["datasourceURI"] = dataSource
confMap["defaultVersion"] = defaultVersion
confMap["versions"] = verMaps
jsonConf, err := json.MarshalIndent(confMap, "", " ")
if err != nil {
return "", errors.Wrap(err, "failed to marshal config maps")
}
Expand Down

0 comments on commit 9496ed5

Please sign in to comment.