gitlab.com/sparetimecoders/build-tools@v0.1.0/pkg/ci/ci.go (about)

     1  package ci
     2  
     3  import (
     4  	"gitlab.com/sparetimecoders/build-tools/pkg/vcs"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  )
     9  
    10  type CI interface {
    11  	Name() string
    12  	// BuildName returns the name of the current build in lowercase
    13  	BuildName() string
    14  	Branch() string
    15  	BranchReplaceSlash() string
    16  	Commit() string
    17  	SetVCS(vcs vcs.VCS)
    18  	Configured() bool
    19  }
    20  
    21  type Common struct {
    22  	VCS vcs.VCS
    23  }
    24  
    25  func (c *Common) SetVCS(vcs vcs.VCS) {
    26  	c.VCS = vcs
    27  }
    28  
    29  func (c *Common) BuildName(name string) string {
    30  	if name != "" {
    31  		return strings.ToLower(name)
    32  	}
    33  	dir, _ := os.Getwd()
    34  	return strings.ToLower(filepath.Base(dir))
    35  }
    36  
    37  func (c *Common) Branch(name string) string {
    38  	if name != "" {
    39  		return name
    40  	}
    41  	return c.VCS.Branch()
    42  }
    43  
    44  func (c *Common) Commit(name string) string {
    45  	if name != "" {
    46  		return name
    47  	}
    48  	return c.VCS.Commit()
    49  }
    50  
    51  func branchReplaceSlash(name string) string {
    52  	return strings.ReplaceAll(strings.ReplaceAll(name, "/", "_"), " ", "_")
    53  }
    54  
    55  func IsValid(c CI) bool {
    56  	return len(c.Commit()) != 0 || len(c.Branch()) != 0
    57  }