Documentation ¶
Index ¶
- Constants
- Variables
- func Download(url, dstDir string, client Client) (string, error)
- func Extract(archivePath string) (string, error)
- func GenerateATemplateManifest(tmplPath string, fec *stdlib.FileExtChecker, excludes []string) (map[string]string, error)
- func GetPlaceholderInput(placeholders *TmplJson, tmplValues *tmplVars, r *os.File, defaultVal string) error
- func ListTemplateFields(t *template.Template, res map[string]string)
- func ManifestParseDir(path string, fec *stdlib.FileExtChecker, excludes []string) ([]string, error)
- func Parse(tplFile, dstDir string, vars tmplVars) error
- func ParseDir(tplDir, outDir string, vars tmplVars, fec *stdlib.FileExtChecker, ...) (err error)
- func ShowAllPlaceholderValues(placeholders *TmplJson, tmplValues *tmplVars)
- func UpdateUserSettings(cfg *Config, mode os.FileMode) error
- func Validate(userInput, placeholder string, validators []validator) (bool, error)
- type AnswersJson
- type Client
- type Config
- type TmplJson
- type UserOptions
Examples ¶
Constants ¶
const ( CmdConfig = "config" CmdManifest = "manifest" DirMode = 0774 PS = string(os.PathSeparator) )
const ( MaxTplSize = 1e+7 EmptyFile = ".empty" )
const (
TmplManifest = "template.json"
)
Variables ¶
var Errors = struct { AnswerFile404 string AppDataDir string BadExcludeFileExt string BadTmplType string CannotDecodeAnswerFile string CannotInitFileChecker string CannotReadAnswerFile string Checkout string Cloning string CouldNot string CouldNotCloseFile string CouldNotDecode string CouldNotEncodeConfig string CouldNotMakeCacheDir string CouldNotSaveConf string CouldNotWriteFile string CurrentBranch string FatalHeader string FlagOrderErr string FileTooBig string GettingAnswers string GettingCommitHash string GitCheckoutFailed string GitFetchFailed string GitExitErrCode string GetLatestTag string GetRemoteTags string InvalidNoArgs string InvalidNoSubCmdArgs string InvalidTmplDir string LocalOutPath string MissingTmplJson string NoGitTagFound string OutPathCollision string ParsingConfigArgs string PathNotAllowed string RunGitFailed string TmplManifest404 string TmplOutput string TmplPath string UnhandledHttpErr string encodingJson string savingManifest string parsingFile string pathNotExist string }{ AnswerFile404: "could not find the answer file %q, please specify a path to a valid answer file that exist: given %q", AppDataDir: "the following error occurred trying to get the app data directory: %q", BadExcludeFileExt: "invalid ExcludeFileExtensions, check format, for example: item1,item2,item3", BadTmplType: "%q is an invalid value for flag tmplType, or it was not set, must be zip|git", CannotDecodeAnswerFile: "could not decode JSON in answer file %q, because of: %s", CannotInitFileChecker: "cannot instantiate file extension checker: %v", CannotReadAnswerFile: "there was an error reading the answer file %q: %s", Checkout: "checkout failed for branch %q", Cloning: "error cloning %v: %s", CouldNot: "could not %s", CouldNotCloseFile: "could not close file %v, %v", CouldNotDecode: "could not decode %q, error: %s", CouldNotEncodeConfig: "could not JSON encode user configuration settings, %v", CouldNotMakeCacheDir: "could not make cache directory, error: %s", CouldNotSaveConf: "could not save a config file, reason: %v", CouldNotWriteFile: "could not write file %v, reason: %v", CurrentBranch: "failed to get current for %s", FatalHeader: "\nfatal error detected: ", FlagOrderErr: "flag %v MUST come before any non-flag arguments, a fix would be to move this flag to the left of other input arguments", FileTooBig: "template file too big to Parse, must be less thatn %v bytes", GettingAnswers: "problem getting answers; error %q", GettingCommitHash: "error getting commit hash %v: %s", GitCheckoutFailed: "git checkout failed: %s", GetLatestTag: "failed to get latest tag from %v: %v", GetRemoteTags: "could not get remote tags, please check for a typo, it exist, and is readable: %v", GitExitErrCode: "git %v returned exit code %q", GitFetchFailed: "fetch failed on %s and %s; %s", InvalidNoArgs: "invalid number of arguments passed to config sub-command, please try config -h for usage", InvalidNoSubCmdArgs: "sub-command %v takes %v number of arguments, try \"%[1]s -h\" for usage", InvalidTmplDir: "invalid template directory %q", LocalOutPath: "enter a local path to output the app", MissingTmplJson: "%s is a file that is required to be in the template, there was a problem reading %q; error %q", NoGitTagFound: "no tag found in %v", OutPathCollision: "-tmpl-path %q and -out-path %q cannot point to the same directory", ParsingConfigArgs: "error parsing config command args: %v", PathNotAllowed: "path/URL to template is not in the allow-list", RunGitFailed: "error running git %v: %v\n%s", TmplManifest404: "the required manifest %q file was not found", TmplOutput: "template has NOT been cloned locally", TmplPath: "please specify a path (or URL) to a template", UnhandledHttpErr: "template Download aborted; I'm coded to NOT do anything when HTTP status is %q and status code is %d", // contains filtered or unexported fields }
var Messages = struct { ActualArgs string CloningToCache string ConfigFileExist string CurrentVersion string CurrentVersionInfo string GitCheckout string MadeNewConfig string NumNonFlagArgs string NumParsedFlags string OutPathExist string OutRepoDir string ProvideValues string PrintAllFlags string PrintFlag string PlaceholderAnswer string PlaceholderAnswerStat string PlaceholderHasAnswer string RefInfo string RemoteTagDbug1 string ReadConfig string RepoDir string RepoInfo string RunningCommand string SaveData string SkipFile string SubCommands string UnknownFileType string UsageHeader string UsingCache string VerboseLevelInfo string }{ ActualArgs: "actual arguments passed in: %v", CloningToCache: "no cache; cloning %v to %v", ConfigFileExist: "config file %q exist", CurrentVersion: "%v, %v", CurrentVersionInfo: "version: %v, %v", GitCheckout: "git checkout %s", MadeNewConfig: "saved %d bytes to a new config %q", NumNonFlagArgs: "number of non-flag arguments passed in: %d", NumParsedFlags: "number of parsed flags = %v", OutRepoDir: "repoDir = %v", OutPathExist: "out-path already exits %q", ProvideValues: "note: entering no value will render the placeholder with an empty string", PrintAllFlags: "printing all flags set:", PrintFlag: "\t%s = %v (default= %v)", PlaceholderAnswer: "%v = %q", PlaceholderAnswerStat: "please provide values for %v placeholders", PlaceholderHasAnswer: "placeholder %v has a value of %q, so skipping", RefInfo: "ref = %v ", RemoteTagDbug1: "remote tag: %v", ReadConfig: "reading config file %v", RepoDir: "repoDir = %q", RepoInfo: "repo = %q; %q", RunningCommand: "running command %s", SaveData: "%save data: s", SkipFile: "skipping: %v", SubCommands: "sub-commands:\n", UsageHeader: "Usage: %v -[options] [args]\n", UsingCache: "using cache %v", UnknownFileType: "will skip and not process through template engine; could not detect file type for %v", VerboseLevelInfo: "verbose level: %v", }
Messages helpful info to std out
Functions ¶
func Download ¶
Download a template from a URL to a local directory.
Example ¶
client := http.Client{} _, err := Download( "https://github.com/kohirens/tmpltoapp-test-tpl/archive/main.zip", TmpDir, &client, ) if err != nil { return }
Output:
func Extract ¶
Example ¶
_, err := Extract( TmpDir + PS + "001.zip", ) if err != nil { return }
Output:
func GenerateATemplateManifest ¶
func GenerateATemplateManifest(tmplPath string, fec *stdlib.FileExtChecker, excludes []string) (map[string]string, error)
GenerateATemplateManifest Make a JSON file with your templates placeholders.
func GetPlaceholderInput ¶
func GetPlaceholderInput(placeholders *TmplJson, tmplValues *tmplVars, r *os.File, defaultVal string) error
GetPlaceholderInput Checks for any missing placeholder values waits for their input from the CLI.
func ListTemplateFields ¶
ListTemplateFields list actions in Go templates. See SO answer: https://stackoverflow.com/a/40584967/419097
func ManifestParseDir ¶
ManifestParseDir Recursively walk a directory parsing all files along the way as Go templates.
func ParseDir ¶
func ParseDir(tplDir, outDir string, vars tmplVars, fec *stdlib.FileExtChecker, excludes []string) (err error)
ParseDir Recursively walk a directory parsing all files along the way as Go templates.
func ShowAllPlaceholderValues ¶
func ShowAllPlaceholderValues(placeholders *TmplJson, tmplValues *tmplVars)
Types ¶
type AnswersJson ¶
type AnswersJson struct {
Placeholders tmplVars `json:"placeholders"`
}
func LoadAnswers ¶
func LoadAnswers(filename string) (*AnswersJson, error)
LoadAnswers Load key/value pairs from a JSON file to fill in placeholders (provides that data for the Go templates).
func NewAnswerJson ¶
func NewAnswerJson() *AnswersJson
type Client ¶
type Client interface { Get(url string) (*http.Response, error) Head(url string) (*http.Response, error) }
Client specify the methods reqruied by an HTTP client
type Config ¶
type Config struct { AnswersJson *AnswersJson // data use for template processing AnswersPath string // flag to get the path to a file containing values to variables to be parsed. OutPath string // flag to set the location of the processed template output. DataDir string // Directory to store app data. DefaultVal string // Flag to set a default placeholder value when a placeholder is empty. TmplPath string // flag to set the URL or local template path to a template. Tmpl string // Path to template, this will be the cached path. TmplJson *TmplJson // Data about the template such as placeholders, their descriptions, version, etc. Branch string // flag to set the desired branch of the template to . SubCmd string // sub-command to execute TmplLocation string // Indicates local or remote location to downloaded TmplType string // Flag to indicate the type of package for a template, such as a zip to Extract or a repository to Download. CurrentVersion string // Current semantic version of the application. CommitHash string // Git commit has of the current version. Help bool // flag to show the usage for all flags. Path string // Path to configuration file. Version bool // flag to show the current version UsrOpts *UserOptions // options that can configured by the user. SubCmdConfig struct { FlagSet *flag.FlagSet Key string // config setting Method string // Method to call Value string // value to update config setting } SubCmdManifest struct { FlagSet *flag.FlagSet Path string // path to generate a manifest for. } }
func (*Config) LoadUserSettings ¶
LoadUserSettings from a file, replacing the default built-in settings.
type TmplJson ¶
type TmplJson struct { Excludes []string `json:"excludes"` Placeholders tmplVars `json:"placeholders"` Validation []validator `json:"validation"` Version string `json:"version"` }
func ReadTemplateJson ¶
ReadTemplateJson read variables needed from the template.json file.
type UserOptions ¶
UserOptions Options the user can set