Apply executes system operations with the following options: - hostname: System hostname - user: Current username - os: Operating system (linux, darwin, windows) - arch: System architecture (amd64, arm64, etc) - env:VALUE: Environment variable lookup - pwd: Current working directory - home: User's h
(operation string, value string)
| 25 | // - pwd: Current working directory |
| 26 | // - home: User's home directory |
| 27 | func (p *SysPlugin) Apply(operation string, value string) (string, error) { |
| 28 | debugf("Sys: operation=%q value=%q", operation, value) |
| 29 | |
| 30 | switch operation { |
| 31 | case "hostname": |
| 32 | hostname, err := os.Hostname() |
| 33 | if err != nil { |
| 34 | debugf("Sys: hostname error: %v", err) |
| 35 | return "", fmt.Errorf(i18n.T("template_sys_error_hostname"), err) |
| 36 | } |
| 37 | debugf("Sys: hostname=%q", hostname) |
| 38 | return hostname, nil |
| 39 | |
| 40 | case "user": |
| 41 | currentUser, err := user.Current() |
| 42 | if err != nil { |
| 43 | debugf("Sys: user error: %v", err) |
| 44 | return "", fmt.Errorf(i18n.T("template_sys_error_user"), err) |
| 45 | } |
| 46 | debugf("Sys: user=%q", currentUser.Username) |
| 47 | return currentUser.Username, nil |
| 48 | |
| 49 | case "os": |
| 50 | result := runtime.GOOS |
| 51 | debugf("Sys: os=%q", result) |
| 52 | return result, nil |
| 53 | |
| 54 | case "arch": |
| 55 | result := runtime.GOARCH |
| 56 | debugf("Sys: arch=%q", result) |
| 57 | return result, nil |
| 58 | |
| 59 | case "env": |
| 60 | if value == "" { |
| 61 | debugf("Sys: env error: missing variable name") |
| 62 | return "", errors.New(i18n.T("template_sys_error_env_requires_var")) |
| 63 | } |
| 64 | result := os.Getenv(value) |
| 65 | debugf("Sys: env %q=%q", value, result) |
| 66 | return result, nil |
| 67 | |
| 68 | case "pwd": |
| 69 | dir, err := os.Getwd() |
| 70 | if err != nil { |
| 71 | debugf("Sys: pwd error: %v", err) |
| 72 | return "", fmt.Errorf(i18n.T("template_sys_error_pwd"), err) |
| 73 | } |
| 74 | debugf("Sys: pwd=%q", dir) |
| 75 | return dir, nil |
| 76 | |
| 77 | case "home": |
| 78 | homeDir, err := os.UserHomeDir() |
| 79 | if err != nil { |
| 80 | debugf("Sys: home error: %v", err) |
| 81 | return "", fmt.Errorf(i18n.T("template_sys_error_home"), err) |
| 82 | } |
| 83 | debugf("Sys: home=%q", homeDir) |
| 84 | return homeDir, nil |