mcl, docs: Use the less ambiguous form of the import

Update the style guide as well!
This commit is contained in:
James Shubin
2025-06-05 22:45:26 -04:00
parent e5ec13f592
commit 75bafa4fd3
11 changed files with 36 additions and 28 deletions

View File

@@ -256,6 +256,15 @@ like: `import "https://github.com/purpleidea/mgmt-banana/"` and namespace it as
`import "https://github.com/purpleidea/mgmt-banana/" as tomato` or something
similar.
### Imports
When importing "golang" modules such as "golang/strings" it's recommended to use
the `import "golang/strings" as golang_strings` format. This is to avoid
confusion with the normal core package you get from `import "strings"`.
In the long-term, we expect to remove the `"golang/"` namespace when our own
standard library is complete enough.
### Licensing
We believe that sharing code helps reduce unnecessary re-invention, so that we

View File

@@ -1,4 +1,4 @@
import "golang/strings"
import "golang/strings" as golang_strings
import "list"
$l1 = ["a", "b", "c", "d", "e", "f", "g",]
@@ -10,7 +10,7 @@ $l6 = ["y", "zed",]
$l = list.concat($l1, $l2, $l3, $l4, $l5, $l6)
$joined = strings.join($l, "-")
$joined = golang_strings.join($l, "-")
print "alphabet" {
msg => "${joined}",

View File

@@ -1,8 +1,8 @@
import "fmt"
import "golang/strings"
import "golang/strings" as golang_strings
$l = ["a", "b", "c",]
$joined = strings.join($l, ", ")
$joined = golang_strings.join($l, ", ")
print "debug" {
msg => "${joined}",

View File

@@ -2,10 +2,10 @@
import "fmt"
import "os"
import "golang/strings"
import "golang/strings" as golang_strings
import "example"
$input = example.str2int(strings.trim_space(os.readfile("/tmp/cpu-count")))
$input = example.str2int(golang_strings.trim_space(os.readfile("/tmp/cpu-count")))
$count = if $input > 8 {
8
} else {

View File

@@ -32,7 +32,6 @@
import "fmt"
import "os"
import "golang/strings"
import "embedded/provisioner" # embedded import
# TODO: get all of the values first from the cli config file, and then a webui

View File

@@ -1,7 +1,7 @@
-- metadata.yaml --
#files: "files/" # these are some extra files we can use (is the default)
-- main.mcl --
import "golang/strings"
import "golang/strings" as golang_strings
import "deploy"
import "second.mcl"
import "mod1/"
@@ -41,9 +41,9 @@ if $x3 != $mod1.x3 {
}
# hide the newlines from our output
test [strings.trim_space($x1),] {}
test [strings.trim_space($x2),] {}
test [strings.trim_space($x3),] {}
test [golang_strings.trim_space($x1),] {}
test [golang_strings.trim_space($x2),] {}
test [golang_strings.trim_space($x3),] {}
# debugging:
#test "f1" {
# anotherstr => $x1,

View File

@@ -1,7 +1,7 @@
-- metadata.yaml --
#files: "files/" # these are some extra files we can use (is the default)
-- main.mcl --
import "golang/strings"
import "golang/strings" as golang_strings
import "deploy"
import "second.mcl"
import "mod1/"
@@ -41,9 +41,9 @@ if $x3 != $mod1.x3 {
}
# hide the newlines from our output
test [strings.trim_space($x1),] {}
test [strings.trim_space($x2),] {}
test [strings.trim_space($x3),] {}
test [golang_strings.trim_space($x1),] {}
test [golang_strings.trim_space($x2),] {}
test [golang_strings.trim_space($x3),] {}
# debugging:
#test "f1" {
# anotherstr => $x1,

View File

@@ -1,7 +1,7 @@
-- metadata.yaml --
#files: "files/" # these are some extra files we can use (is the default)
-- main.mcl --
import "golang/strings"
import "golang/strings" as golang_strings
import "deploy"
import "second.mcl"
@@ -31,8 +31,8 @@ if $x2 != $second.x2 {
}
# hide the newlines from our output
test [strings.trim_space($x1),] {}
test [strings.trim_space($x2),] {}
test [golang_strings.trim_space($x1),] {}
test [golang_strings.trim_space($x2),] {}
-- second.mcl --
import "deploy"

View File

@@ -1,7 +1,7 @@
-- metadata.yaml --
#files: "files/" # these are some extra files we can use (is the default)
-- main.mcl --
import "golang/strings"
import "golang/strings" as golang_strings
import "deploy"
$f1 = "/files/file1"
@@ -19,7 +19,7 @@ if $x1 != deploy.readfile($f1) {
}
# hide the newlines from our output
test [strings.trim_space($x1),] {}
test [golang_strings.trim_space($x1),] {}
-- files/file1 --
This is file1 in the files/ folder.
-- OUTPUT --

View File

@@ -31,7 +31,7 @@ import "deploy"
import "fmt"
import "golang"
import "local"
import "golang/strings"
import "golang/strings" as golang_strings
import "os"
class server($st) {
@@ -146,7 +146,7 @@ class server:subnet($name, $st) {
netmask => "${netmask}",
broadcast => "${broadcast}",
router => "${router}", # TODO: support multiple
dns => strings.join($dns, ", "),
dns => golang_strings.join($dns, ", "),
range => $valid_range,
#nextserver => "${nextserver}",
#filename => "${filename}",
@@ -206,7 +206,7 @@ class server:host($name, $st) {
$tmpl = struct{
name => "${name}",
macaddress => "${macaddress}",
valid_fixedaddress => strings.join($fixedaddress, ","),
valid_fixedaddress => golang_strings.join($fixedaddress, ","),
#fixedaddress => $fixedaddress, # TODO: when it's a list
hostname => "${hostname}",
nextserver => "${nextserver}",

View File

@@ -31,7 +31,7 @@ import "deploy"
import "fmt"
import "golang"
import "local"
import "golang/strings"
import "golang/strings" as golang_strings
# Class prepare adds some common things you probably want to run when using this
@@ -199,7 +199,7 @@ class firewall:interface($name, $zone, $st) {
print "interface: ${name}" {}
include interface_base
$interface = $st->interface || (strings.to_upper($zone) + "_IF") # eg: NET_IF
$interface = $st->interface || (golang_strings.to_upper($zone) + "_IF") # eg: NET_IF
$physical = $st->physical || $name
$options []str = $st->options || [] # TODO: add option validation?
$comment = $st->comment || ""
@@ -328,14 +328,14 @@ class firewall:rule($name, $st) {
#$original = $st->original || [] # TODO
$comment = $st->comment || ""
$source_ips_joined = strings.join($source_ips, ",")
$source_ips_joined = golang_strings.join($source_ips, ",")
$valid_source = if $source_ips_joined == "" {
"${source}"
} else {
"${source}:${source_ips_joined}"
}
$dest_ips_joined = strings.join($dest_ips, ",")
$dest_ips_joined = golang_strings.join($dest_ips, ",")
$valid_dest = if $dest_ips_joined == "" {
"${dest}"
} else {
@@ -506,7 +506,7 @@ class firewall:snat($name, $st) {
$port = $st->port || 0
$comment = $st->comment || ""
$valid_source = strings.join($source, ",")
$valid_source = golang_strings.join($source, ",")
$valid_proto = if $proto == "" {
"-"