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