github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/command/cloudbuild.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/cloudbuild"
    25  	"github.com/drone/go-convert/convert/harness/downgrader"
    26  
    27  	"github.com/google/subcommands"
    28  )
    29  
    30  type Cloudbuild struct {
    31  	name       string
    32  	proj       string
    33  	org        string
    34  	repoName   string
    35  	repoConn   string
    36  	kubeName   string
    37  	kubeConn   string
    38  	dockerConn string
    39  
    40  	downgrade   bool
    41  	beforeAfter bool
    42  }
    43  
    44  func (*Cloudbuild) Name() string     { return "cloudbuild" }
    45  func (*Cloudbuild) Synopsis() string { return "converts a cloudbuild pipeline" }
    46  func (*Cloudbuild) Usage() string {
    47  	return `cloudbuild [-downgrade] [cloudbuild.yaml]
    48  `
    49  }
    50  
    51  func (c *Cloudbuild) SetFlags(f *flag.FlagSet) {
    52  	f.BoolVar(&c.downgrade, "downgrade", false, "downgrade to the legacy yaml format")
    53  	f.BoolVar(&c.beforeAfter, "before-after", false, "print the befor and after")
    54  
    55  	f.StringVar(&c.org, "org", "default", "harness organization")
    56  	f.StringVar(&c.proj, "project", "default", "harness project")
    57  	f.StringVar(&c.name, "pipeline", "default", "harness pipeline name")
    58  	f.StringVar(&c.repoConn, "repo-connector", "", "repository connector")
    59  	f.StringVar(&c.repoName, "repo-name", "", "repository name")
    60  	f.StringVar(&c.kubeConn, "kube-connector", "", "kubernetes connector")
    61  	f.StringVar(&c.kubeName, "kube-namespace", "", "kubernets namespace")
    62  	f.StringVar(&c.dockerConn, "docker-connector", "", "dockerhub connector")
    63  }
    64  
    65  func (c *Cloudbuild) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
    66  	path := f.Arg(0)
    67  
    68  	// if the user does not specify the path as
    69  	// a command line arg, assume the default path.
    70  	if path == "" {
    71  		path = "cloudbuild.yaml"
    72  	}
    73  
    74  	// open the cloudbuild yaml
    75  	before, err := ioutil.ReadFile(path)
    76  	if err != nil {
    77  		log.Println(err)
    78  		return subcommands.ExitFailure
    79  	}
    80  
    81  	// convert the pipeline yaml from the cloudbuild
    82  	// format to the harness yaml format.
    83  	converter := cloudbuild.New(
    84  		cloudbuild.WithDockerhub(c.dockerConn),
    85  		cloudbuild.WithKubernetes(c.kubeName, c.kubeConn),
    86  	)
    87  	after, err := converter.ConvertBytes(before)
    88  	if err != nil {
    89  		log.Println(err)
    90  		return subcommands.ExitFailure
    91  	}
    92  
    93  	// downgrade from the v1 harness yaml format
    94  	// to the v0 harness yaml format.
    95  	if c.downgrade {
    96  		// downgrade to the v0 yaml
    97  		d := downgrader.New(
    98  			downgrader.WithCodebase(c.repoName, c.repoConn),
    99  			downgrader.WithDockerhub(c.dockerConn),
   100  			downgrader.WithKubernetes(c.kubeName, c.kubeConn),
   101  			downgrader.WithName(c.name),
   102  			downgrader.WithOrganization(c.org),
   103  			downgrader.WithProject(c.proj),
   104  		)
   105  		after, err = d.Downgrade(after)
   106  		if err != nil {
   107  			log.Println(err)
   108  			return subcommands.ExitFailure
   109  		}
   110  	}
   111  
   112  	if c.beforeAfter {
   113  		os.Stdout.WriteString("---\n")
   114  		os.Stdout.Write(before)
   115  		os.Stdout.WriteString("\n---\n")
   116  	}
   117  
   118  	os.Stdout.Write(after)
   119  
   120  	return subcommands.ExitSuccess
   121  }