Skip to content
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
11 changes: 10 additions & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,15 @@ var initCmd = &cobra.Command{
removeUneededArtifactRepositoryProviders(mergedParams)

mergedParams.Sort()

inputCommand := "# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n"
inputCommand += "# Generated with Onepanel CLI \n"
inputCommand += "# Command: opctl " + strings.Join(os.Args[1:], " ") + "\n"
inputCommand += "# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
if err := mergedParams.SetTopComment(inputCommand); err != nil {
log.Printf("[error] setting comments: %v", err.Error())
return
}
paramsString, err := mergedParams.String()
if err != nil {
log.Printf("[error] unable to write params to a string")
Expand Down Expand Up @@ -445,7 +454,7 @@ func removeUneededArtifactRepositoryProviders(mergedParams *util.DynamicYaml) {
}

parentValue := mergedParams.GetValue("artifactRepository")
if len(parentValue.Content) == 0 {
if parentValue != nil && len(parentValue.Content) == 0 {
if err := mergedParams.Delete("artifactRepository"); err != nil {
log.Printf("error during init, artifact repository provider. %v", err)
}
Expand Down
28 changes: 26 additions & 2 deletions util/dynamic_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ func LoadDynamicYamlFromString(input string) (*DynamicYaml, error) {
return dynamicYaml, nil
}

// SetTopComment sets the topmost comment in the resulting yaml
func (d *DynamicYaml) SetTopComment(comment string) error {
if d == nil {
return nil
}

if d.node == nil {
return fmt.Errorf("DynamicYaml has not been loaded yet")
}

d.node.HeadComment = comment

return nil
}

func (d *DynamicYaml) GetByParts(parts ...string) (key, value *yaml.Node) {
if len(d.node.Content) == 0 {
return nil, nil
Expand Down Expand Up @@ -608,9 +623,10 @@ func (d *DynamicYaml) mergeSingle(y *DynamicYaml) {
valueNode := values.Content[i+1]

alreadyExists := false
var jValue *yaml.Node = nil
var jKey *yaml.Node
var jValue *yaml.Node
for j := 0; j < len(destination.Content)-1; j++ {
jKey := destination.Content[j]
jKey = destination.Content[j]
jValue = destination.Content[j+1]

if keyNode.Value == jKey.Value {
Expand All @@ -620,6 +636,14 @@ func (d *DynamicYaml) mergeSingle(y *DynamicYaml) {
}

if alreadyExists {
if jKey != nil {
// Always replace the comments with the new comment values
// This makes it less generic, but it makes the source YAML comments
// always overwrite any custom yaml, which is the expected behavior.
jKey.HeadComment = keyNode.HeadComment
jKey.LineComment = keyNode.LineComment
jKey.FootComment = keyNode.FootComment
}
mergeNodes(jValue, valueNode)
} else {
destination.Content = append(destination.Content, keyNode)
Expand Down