github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/circle/internal/orbs/golang/golang.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 golang
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	circle "github.com/drone/go-convert/convert/circle/yaml"
    22  	harness "github.com/drone/spec/dist/go"
    23  )
    24  
    25  //
    26  // https://circleci.com/developer/orbs/orb/circleci/go
    27  //
    28  
    29  // Convert converts an Orb to a Harness step.
    30  func Convert(command string, step *circle.Custom) *harness.Step {
    31  	switch command {
    32  	case "install":
    33  		return convertInstall(step)
    34  	case "test":
    35  		return convertTest(step)
    36  	default:
    37  		return nil
    38  	}
    39  }
    40  
    41  func convertInstall(step *circle.Custom) *harness.Step {
    42  	return &harness.Step{
    43  		Name: "go_install",
    44  		Type: "script",
    45  		Spec: &harness.StepExec{
    46  			Run: "go install ./...",
    47  		},
    48  	}
    49  }
    50  
    51  func convertTest(step *circle.Custom) *harness.Step {
    52  	parts := []string{
    53  		"go test -cover",
    54  	}
    55  
    56  	if v, ok := step.Params["covermode"]; ok {
    57  		parts = append(parts, fmt.Sprintf("-covermode %v", v))
    58  	}
    59  
    60  	if v, ok := step.Params["coverpkg"]; ok {
    61  		parts = append(parts, fmt.Sprintf("-covermode %v", v))
    62  	}
    63  
    64  	if v, ok := step.Params["count"]; ok {
    65  		parts = append(parts, fmt.Sprintf("-count %v", v))
    66  	}
    67  
    68  	if v, ok := step.Params["parallel"]; ok {
    69  		parts = append(parts, fmt.Sprintf("-parallel %v", v))
    70  	}
    71  
    72  	if v, _ := step.Params["verbose"].(bool); v {
    73  		parts = append(parts, "-v")
    74  	}
    75  
    76  	if v, _ := step.Params["race"].(bool); v {
    77  		parts = append(parts, "-race")
    78  	}
    79  
    80  	if v, _ := step.Params["short"].(bool); v {
    81  		parts = append(parts, "-short")
    82  	}
    83  
    84  	if v, ok := step.Params["packages"]; ok {
    85  		parts = append(parts, fmt.Sprint(v))
    86  	} else {
    87  		parts = append(parts, "./...")
    88  	}
    89  
    90  	return &harness.Step{
    91  		Name: "go_test",
    92  		Type: "script",
    93  		Spec: &harness.StepExec{
    94  			Run: strings.Join(parts, " "),
    95  		},
    96  	}
    97  }