Skip to content

Commit

Permalink
feat: Create a Nushell script for 'files' module
Browse files Browse the repository at this point in the history
  • Loading branch information
gmpinder committed Jan 19, 2025
1 parent 8ceb728 commit 26467d3
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build-individual.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: build-individual
on:
push:
branches:
- main
paths-ignore: # don't rebuild if only documentation has changed
- "**.md"
pull_request:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/build-unified.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: build-unified
on:
push:
branches:
- main
paths-ignore: # don't rebuild if only documentation has changed
- "**.md"
pull_request:
Expand Down
38 changes: 28 additions & 10 deletions modules/files/files.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,33 @@ import "@typespec/json-schema";
using TypeSpec.JsonSchema;

@jsonSchema("/modules/files.json")
model FilesModule {
/** Copy files to your image at build time
* https://blue-build.org/reference/modules/files/
*/
type: "files";
union FilesModule {
FilesV1,
FilesV2,
}

model FilesV1 {
/** Copy files to your image at build time
* https://blue-build.org/reference/modules/files/
*/
type: "files@v1";

/** List of files / folders to copy. */
files: Array<Record<string>> | Array<{
source: string;
destination: string;
}>;
}

model FilesV2 {
/** Copy files to your image at build time
* https://blue-build.org/reference/modules/files/
*/
type: "files@v2" | "files@latest" | "files";

/** List of files / folders to copy. */
files: Array<Record<string>> | Array<{
source: string;
destination: string;
}>;
/** List of files / folders to copy. */
files: Array<{
source: string;
destination: string;
}>;
}
File renamed without changes.
72 changes: 72 additions & 0 deletions modules/files/v2/files.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env nu

def determine_file_dir []: nothing -> string {
let config_dir = $env.CONFIG_DIRECTORY

if $config_dir == '/tmp/config' {
$config_dir | path join 'files'
} else {
$config_dir
}
}

def main [config: string]: nothing -> nothing {
let config = $config | from json
let files: list = $config.files
let list_is_empty = $files | is-empty

if $list_is_empty {
return (error make {
msg: $"(ansi red_bold)At least one entry is required in property(ansi reset) `(ansi cyan)files(ansi reset)`:\n($config | to yaml)"
label: {
text: 'Checks for empty list'
span: (metadata $list_is_empty).span
}
})
}

let config_dir = determine_file_dir

for $file in $files {
let file = $file | merge { source: ($config_dir | path join $file.source) }
let source = $file.source
let destination = $file.destination
let source_exists = not ($source | path exists)
let is_dir = ($destination | path exists) and ($destination | path type) == 'file'

if $source_exists {
return (error make {
msg: $"(ansi red_bold)The path (ansi cyan)`($source)`(ansi reset) (ansi red_bold)does not exist(ansi reset):\n($config | to yaml)"
label: {
text: 'Checks for source'
span: (metadata $source_exists).span
}
})
}

if $is_dir {
return (error make {
msg: $"(ansi red_bold)The destination path (ansi cyan)`($destination)`(ansi reset) (ansi red_bold)should be a directory(ansi reset):\n($config | to yaml)"
label: {
text: 'Checks destination is directory'
span: (metadata $is_dir).span
}
})
}

print $'Copying (ansi cyan)($source)(ansi reset) to (ansi cyan)($destination)(ansi reset)'
mkdir $destination

if ($source | path type) == 'dir' {
cp -rfv ($source | path join * | into glob) $destination
} else {
cp -fv $source $destination
}

let git_keep = $destination | path join '.gitkeep'

if ($git_keep | path exists) {
rm -f $git_keep
}
}
}

0 comments on commit 26467d3

Please sign in to comment.