github.com/konsorten/ktn-build-info@v1.0.11/ver/inputs.go (about)

     1  package ver
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	log "github.com/sirupsen/logrus"
     8  )
     9  
    10  type InputAction = func(vi *VersionInformation, params map[string]string) error
    11  
    12  type InputSpec struct {
    13  	Name        string
    14  	Description string
    15  	Parameters  []string
    16  	Action      InputAction
    17  }
    18  
    19  var AllInputs = []InputSpec{
    20  	InputSpec{
    21  		Name:        "version-info",
    22  		Description: fmt.Sprintf("Read project and version information from an existing %v file in the current or parent directories.", VersionInfoYamlFilename),
    23  		Parameters: []string{
    24  			"depth:{levels}\tLimit depth of directories to scan. Set to 0 for current directory, only. Default is 10.",
    25  		},
    26  		Action: func(vi *VersionInformation, params map[string]string) error {
    27  			// retrieve max depth
    28  			maxDepth := 10
    29  
    30  			if params["depth"] != "" {
    31  				md, err := strconv.Atoi(params["depth"])
    32  
    33  				if err != nil {
    34  					log.Errorf("Failed to parse 'maxDepth' parameter: %v", params["depth"])
    35  					return err
    36  				}
    37  
    38  				maxDepth = md
    39  			}
    40  
    41  			// run
    42  			ver, err := TryReadFromVersionInfoYAML(maxDepth)
    43  
    44  			if err != nil {
    45  				return err
    46  			}
    47  
    48  			vi.CopyMissingFrom(ver)
    49  
    50  			return nil
    51  		},
    52  	},
    53  	InputSpec{
    54  		Name:        "directory-name",
    55  		Description: "Use the working directory's name as project name.",
    56  		Action: func(vi *VersionInformation, params map[string]string) error {
    57  			// run
    58  			ver, err := TryReadFromWorkingDirectory()
    59  
    60  			if err != nil {
    61  				return err
    62  			}
    63  
    64  			vi.CopyMissingFrom(ver)
    65  
    66  			return nil
    67  		},
    68  	},
    69  	InputSpec{
    70  		Name:        "build-host",
    71  		Description: "Use the current machine's name and time as build host and timestamp.",
    72  		Action: func(vi *VersionInformation, params map[string]string) error {
    73  			// run
    74  			ver, err := TryReadFromBuildHost()
    75  
    76  			if err != nil {
    77  				return err
    78  			}
    79  
    80  			vi.CopyMissingFrom(ver)
    81  
    82  			return nil
    83  		},
    84  	},
    85  	InputSpec{
    86  		Name:        "consul-build-id",
    87  		Description: "Retrieves a build number based on the build revision. Use this for non-numeric revisions, like Git.",
    88  		Parameters: []string{
    89  			"url:{consulurl}\tThe connection URL to consul, e.g. http://consul:8500/dc1 or http://:token@consul:8500/dc1.",
    90  			"root:{key}\tThe base KV path for the project, e.g. builds/myproject.",
    91  		},
    92  		Action: func(vi *VersionInformation, params map[string]string) error {
    93  			return RetrieveBuildFromConsul(params["url"], params["root"], vi)
    94  		},
    95  	},
    96  	InputSpec{
    97  		Name:        "npm",
    98  		Description: fmt.Sprintf("Read project and version information from an existing %v file in the current directory.", PackageJsonFilename),
    99  		Parameters: []string{
   100  			"name:false\tIgnore the project name.",
   101  			"desc:false\tIgnore the project description.",
   102  			"license:false\tIgnore the project license information.",
   103  			"version:false\tIgnore the project version number.",
   104  			"author:false\tIgnore the project author information.",
   105  		},
   106  		Action: func(vi *VersionInformation, params map[string]string) error {
   107  			// run
   108  			ver, err := TryReadFromPackageJSON(
   109  				params["name"] == "false",
   110  				params["version"] == "false",
   111  				params["desc"] == "false",
   112  				params["author"] == "false",
   113  				params["license"] == "false",
   114  			)
   115  
   116  			if err != nil {
   117  				return err
   118  			}
   119  
   120  			vi.CopyMissingFrom(ver)
   121  
   122  			return nil
   123  		},
   124  	},
   125  	InputSpec{
   126  		Name:        "konsorten",
   127  		Description: "Use marvin + konsorten default author information.",
   128  		Action: func(vi *VersionInformation, params map[string]string) error {
   129  			// run
   130  			ver, err := TryReadFromKonsortenDefaults()
   131  
   132  			if err != nil {
   133  				return err
   134  			}
   135  
   136  			vi.CopyMissingFrom(ver)
   137  
   138  			return nil
   139  		},
   140  	},
   141  	InputSpec{
   142  		Name:        "teamcity",
   143  		Description: "Read project name, version, and revision information from TeamCity environment variables.",
   144  		Parameters: []string{
   145  			"build:false\tIgnore the build number.",
   146  			"rev:false\tIgnore the revision number.",
   147  			"name:false\tIgnore the project name.",
   148  		},
   149  		Action: func(vi *VersionInformation, params map[string]string) error {
   150  			// run
   151  			ver, err := TryReadFromTeamCity(
   152  				params["build"] == "false",
   153  				params["rev"] == "false",
   154  				params["name"] == "false",
   155  			)
   156  
   157  			if err != nil {
   158  				return err
   159  			}
   160  
   161  			vi.CopyMissingFrom(ver)
   162  
   163  			return nil
   164  		},
   165  	},
   166  	InputSpec{
   167  		Name:        "gitlab-ci",
   168  		Description: "Read project name and revision information from GitLab CI environment variables.",
   169  		Parameters: []string{
   170  			"rev:false\tIgnore the revision number.",
   171  			"name:false\tIgnore the project name.",
   172  		},
   173  		Action: func(vi *VersionInformation, params map[string]string) error {
   174  			// run
   175  			ver, err := TryReadFromGitlabCI(
   176  				params["rev"] == "false",
   177  				params["name"] == "false",
   178  			)
   179  
   180  			if err != nil {
   181  				return err
   182  			}
   183  
   184  			vi.CopyMissingFrom(ver)
   185  
   186  			return nil
   187  		},
   188  	},
   189  	InputSpec{
   190  		Name:        "git",
   191  		Description: "Read revision information from current directory's Git repository.",
   192  		Action: func(vi *VersionInformation, params map[string]string) error {
   193  			// run
   194  			ver, err := TryReadFromGit()
   195  
   196  			if err != nil {
   197  				return err
   198  			}
   199  
   200  			vi.CopyMissingFrom(ver)
   201  
   202  			return nil
   203  		},
   204  	},
   205  	InputSpec{
   206  		Name:        "env-vars",
   207  		Description: "Read build information from environment variables.",
   208  		Parameters: []string{
   209  			"name:{envVarName}\tRead the project name from the specified environment variable {envVarName}. Ignored if missing or empty.",
   210  			"desc:{envVarName}\tRead the project description from the specified environment variable {envVarName}. Ignored if missing or empty.",
   211  			"rev:{envVarName}\tRead the build revision from the specified environment variable {envVarName}. Ignored if missing or empty.",
   212  			"build:{envVarName}\tRead the build ID from the specified environment variable {envVarName}. Ignored if missing or empty.",
   213  			"buildHost:{envVarName}\tRead the build host name from the specified environment variable {envVarName}. Ignored if missing or empty.",
   214  		},
   215  		Action: func(vi *VersionInformation, params map[string]string) error {
   216  			// run
   217  			ver, err := TryReadFromEnvironmentVariables(params)
   218  
   219  			if err != nil {
   220  				return err
   221  			}
   222  
   223  			vi.CopyMissingFrom(ver)
   224  
   225  			return nil
   226  		},
   227  	},
   228  	InputSpec{
   229  		Name:        "limit-revision",
   230  		Description: "Limits the length of the revision string.",
   231  		Parameters: []string{
   232  			"length:{int}\tThe number of characters to limit the revision string, e.g. 7 for Git short revision.",
   233  		},
   234  		Action: func(vi *VersionInformation, params map[string]string) error {
   235  			l, err := strconv.Atoi(params["length"])
   236  
   237  			if err != nil {
   238  				return err
   239  			}
   240  
   241  			vi.LimitRevision(l)
   242  
   243  			return nil
   244  		},
   245  	},
   246  }
   247  
   248  func GetInputSpec(name string) *InputSpec {
   249  	for _, s := range AllInputs {
   250  		if s.Name == name {
   251  			return &s
   252  		}
   253  	}
   254  
   255  	return nil
   256  }