-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose custom funcs to template (#638)
Having exposed a subset of hardware data to templates there's a need to provide some helper functions for manipulating the data. The concrete use case is formatting of disk partitions. This PR introduces `formatPartition` to templates accepting the device path. The function currently supports historic block devices (`/dev/sd<char>`) and NVMe drives. Additionally it adds `strings.Contains`, `strings.HasPrefix` and `strings.HasSuffix` which have proven useful. **Example template snippet** ``` name: {{ .Hardware.Name }} disk: {{ formatPartition ( index .Hardware.Disks 0 ) 1 }} ``` Documentation will need adding to the docs website in the on-going documentation effort.
- Loading branch information
Showing
4 changed files
with
387 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package workflow | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// templateFuncs defines the custom functions available to workflow templates. | ||
var templateFuncs = map[string]interface{}{ | ||
"contains": strings.Contains, | ||
"hasPrefix": strings.HasPrefix, | ||
"hasSuffix": strings.HasSuffix, | ||
"formatPartition": formatPartition, | ||
} | ||
|
||
// formatPartition formats a device path with partition for the device type. If it receives an | ||
// unidentifiable device path it returns the dev. | ||
// | ||
// Examples | ||
// formatPartition("/dev/nvme0n1", 0) -> /dev/nvme0n1p1 | ||
// formatPartition("/dev/sda", 1) -> /dev/sda1 | ||
func formatPartition(dev string, partition int) string { | ||
switch { | ||
case strings.HasPrefix(dev, "/dev/nvme"): | ||
return fmt.Sprintf("%vp%v", dev, partition) | ||
case strings.HasPrefix(dev, "/dev/sd"): | ||
return fmt.Sprintf("%v%v", dev, partition) | ||
} | ||
return dev | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.