github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/command/downgrade.go (about) 1 // Copyright 2022 Harness, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package command 16 17 import ( 18 "context" 19 "flag" 20 "io/ioutil" 21 "log" 22 "os" 23 24 "github.com/drone/go-convert/convert/harness/downgrader" 25 26 "github.com/google/subcommands" 27 ) 28 29 type Downgrade struct { 30 name string 31 proj string 32 org string 33 repoName string 34 repoConn string 35 kubeName string 36 kubeConn string 37 dockerConn string 38 39 downgrade bool 40 beforeAfter bool 41 } 42 43 func (*Downgrade) Name() string { return "downgrade" } 44 func (*Downgrade) Synopsis() string { return "converts a harness pipeline to the v0 format" } 45 func (*Downgrade) Usage() string { 46 return `downgrade <path to harness yaml> 47 ` 48 } 49 50 func (c *Downgrade) SetFlags(f *flag.FlagSet) { 51 f.StringVar(&c.org, "org", "default", "harness organization") 52 f.StringVar(&c.proj, "project", "default", "harness project") 53 f.StringVar(&c.name, "pipeline", "default", "harness pipeline name") 54 f.StringVar(&c.repoConn, "repo-connector", "", "repository connector") 55 f.StringVar(&c.repoName, "repo-name", "", "repository name") 56 f.StringVar(&c.kubeConn, "kube-connector", "", "kubernetes connector") 57 f.StringVar(&c.kubeName, "kube-namespace", "", "kubernets namespace") 58 f.StringVar(&c.dockerConn, "docker-connector", "", "dockerhub connector") 59 } 60 61 func (c *Downgrade) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { 62 path := f.Arg(0) 63 64 var before []byte 65 var err error 66 67 // if the user provides the yaml path, 68 // read the yaml file. 69 if path != "" { 70 before, err = ioutil.ReadFile(path) 71 if err != nil { 72 log.Println(err) 73 return subcommands.ExitFailure 74 } 75 76 } else { 77 // else read the yaml file from stdin 78 before, _ = ioutil.ReadAll(os.Stdin) 79 } 80 81 // downgrade to the v0 yaml 82 d := downgrader.New( 83 downgrader.WithCodebase(c.repoName, c.repoConn), 84 downgrader.WithDockerhub(c.dockerConn), 85 downgrader.WithKubernetes(c.kubeName, c.kubeConn), 86 downgrader.WithName(c.name), 87 downgrader.WithOrganization(c.org), 88 downgrader.WithProject(c.proj), 89 ) 90 after, err := d.Downgrade(before) 91 if err != nil { 92 log.Println(err) 93 return subcommands.ExitFailure 94 } 95 96 if c.beforeAfter { 97 os.Stdout.WriteString("---\n") 98 os.Stdout.Write(before) 99 os.Stdout.WriteString("\n---\n") 100 } 101 102 os.Stdout.Write(after) 103 104 return subcommands.ExitSuccess 105 }