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

     1  package ver
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strconv"
     7  	"strings"
     8  )
     9  
    10  type OutputAction = func(vi *VersionInformation, params map[string]string) error
    11  
    12  type OutputSpec struct {
    13  	Name        string
    14  	Description string
    15  	Parameters  []string
    16  	Action      OutputAction
    17  }
    18  
    19  var AllOutputs = []OutputSpec{
    20  	OutputSpec{
    21  		Name:        "template",
    22  		Description: "Renders a template file and writes the result into a file dropping the last extension, e.g. myfile.c.template becomes myfile.c. Takes the relative file path as parameter.",
    23  		Parameters: []string{
    24  			"file:{path}\tPath to the template file.",
    25  			"mode:{filemode}\tFile mode to use for the file (Linux and macOS). Typical values are 644 for a regular file and 755 for an executable file.",
    26  		},
    27  		Action: func(vi *VersionInformation, params map[string]string) error {
    28  			// determine filename
    29  			f := params["file"]
    30  
    31  			if f == "" {
    32  				return fmt.Errorf("No template file specified; missing 'file' parameter")
    33  			}
    34  
    35  			// parse the file mode
    36  			var fm os.FileMode = 0644
    37  
    38  			fmStr := params["mode"]
    39  
    40  			if fmStr != "" {
    41  				fmParsed, err := strconv.ParseUint(fmStr, 8, 32)
    42  
    43  				if err != nil {
    44  					return fmt.Errorf("Failed to parse file mode: %v: %v", fmStr, err)
    45  				}
    46  
    47  				fm = os.FileMode(fmParsed)
    48  			}
    49  
    50  			return vi.WriteTemplateFile(f, fm)
    51  		},
    52  	},
    53  	OutputSpec{
    54  		Name:        "update-npm",
    55  		Description: fmt.Sprintf("Updates an existing NPM %v file in the current directory.", PackageJsonFilename),
    56  		Action: func(vi *VersionInformation, params map[string]string) error {
    57  			return UpdatePackageJSON(vi)
    58  		},
    59  	},
    60  	OutputSpec{
    61  		Name:        "update-json",
    62  		Description: "Updates an existing JSON document.",
    63  		Parameters: []string{
    64  			"file:{path}\tPath to the JSON file.",
    65  			"indent:{chars}\tCharacters to use for indent, like four space characters.",
    66  			"!{path}:{value}\tSet the value at {path} to {value}. All template file fields are supported. Strings have to be quoted, e.g. '\"{$.Author$}\"' or '{$.Author | asQuoted$}'. Missing objects are created automatically.",
    67  			"!{path}:$null$\tSets the value at {path} to null.",
    68  			"!{path}:$delete$\tDeletes the value at {path}.",
    69  		},
    70  		Action: func(vi *VersionInformation, params map[string]string) error {
    71  			f := params["file"]
    72  
    73  			if f == "" {
    74  				return fmt.Errorf("No JSON file specified; missing 'file' parameter")
    75  			}
    76  
    77  			// gather actions
    78  			actions := make(UpdateActions)
    79  
    80  			for k, v := range params {
    81  				if strings.HasPrefix(k, "!") {
    82  					actions[k[1:]] = v
    83  				}
    84  			}
    85  
    86  			return UpdateJsonFile(f, actions, vi, params["indent"])
    87  		},
    88  	},
    89  	OutputSpec{
    90  		Name:        "update-xml",
    91  		Description: "Updates an existing XML document.",
    92  		Parameters: []string{
    93  			"file:{path}\tPath to the XML file.",
    94  			"indent:{chars}\tCharacters to use for indent, like four space characters.",
    95  			"!{xpath}:{value}\tSet the value at {xpath} to {value}. The target element/attribute must exist. All template file fields are supported.",
    96  			"!{xpath}:$create$\tCreates a new element or attribute from {xpath}. The parent element must exist.",
    97  			"!{xpath}:$ensure$\tIf missing, creates a new element or attribute from {xpath}. The parent element must exist.",
    98  			"!{xpath}:$delete$\tDeletes the value at {xpath}.",
    99  		},
   100  		Action: func(vi *VersionInformation, params map[string]string) error {
   101  			f := params["file"]
   102  
   103  			if f == "" {
   104  				return fmt.Errorf("No XML file specified; missing 'file' parameter")
   105  			}
   106  
   107  			// gather actions
   108  			actions := make(UpdateActions)
   109  
   110  			for k, v := range params {
   111  				if strings.HasPrefix(k, "!") {
   112  					actions[k[1:]] = v
   113  				}
   114  			}
   115  
   116  			return UpdateXmlFile(f, actions, vi, params["indent"])
   117  		},
   118  	},
   119  	OutputSpec{
   120  		Name:        "update-regex",
   121  		Description: "Updates an existing text document.",
   122  		Parameters: []string{
   123  			"file:{path}\tPath to the text file.",
   124  			"posix:true\tRestricts the regular expression to POSIX ERE (egrep) syntax.",
   125  			"!{regex}:{replace}\tReplaces all matches of {regex} with {replace}. All template file fields are supported.",
   126  		},
   127  		Action: func(vi *VersionInformation, params map[string]string) error {
   128  			f := params["file"]
   129  
   130  			if f == "" {
   131  				return fmt.Errorf("No text file specified; missing 'file' parameter")
   132  			}
   133  
   134  			// gather actions
   135  			actions := make(UpdateActions)
   136  
   137  			for k, v := range params {
   138  				if strings.HasPrefix(k, "!") {
   139  					actions[k[1:]] = v
   140  				}
   141  			}
   142  
   143  			return UpdateRegexFile(f, actions, params["posix"] == "true", vi)
   144  		},
   145  	},
   146  	OutputSpec{
   147  		Name:        "go-syso",
   148  		Description: fmt.Sprintf("Writes the version information to be embedded into an Go executable (%v). Supported on Windows, only.", goSysoFilename),
   149  		Action: func(vi *VersionInformation, params map[string]string) error {
   150  			return WriteGoSysoFile(vi)
   151  		},
   152  	},
   153  	OutputSpec{
   154  		Name:        "teamcity",
   155  		Description: "Writes the version number back to TeamCity.",
   156  		Action: func(vi *VersionInformation, params map[string]string) error {
   157  			return vi.WriteToTeamCity()
   158  		},
   159  	},
   160  }
   161  
   162  func GetOutputSpec(name string) *OutputSpec {
   163  	for _, s := range AllOutputs {
   164  		if s.Name == name {
   165  			return &s
   166  		}
   167  	}
   168  
   169  	return nil
   170  }