Seu Primeiro Invowkfile
Agora que você executou seu primeiro comando, vamos criar algo mais prático. Criaremos um invowkfile para um projeto típico com comandos de build, teste e deploy.
Entendendo a Estrutura
Um invowkfile tem uma estrutura simples:
// invowkfile.cue (commands only)
cmds: [ // Required: list of commands
// ... your commands here
]
Invowkfiles definem comandos diretamente. Se você quiser namespacing, use um módulo e defina o ID do módulo em invowkmod.cue.
Um Exemplo do Mundo Real
Vamos criar um invowkfile para um projeto Go:
cmds: [
// Simple build command
{
name: "build"
description: "Build the project"
implementations: [
{
script: {content: """
echo "Building..."
go build -o bin/app ./...
echo "Done! Binary at bin/app"
"""}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
}
]
},
// Test command with subcommand-style naming
{
name: "test unit"
description: "Run unit tests"
implementations: [
{
script: {content: "go test -v ./..."}
runtimes: [
{name: "native"},
{name: "virtual-sh", allowed_binaries: ["go"], binary_lookup_mode: "host"},
]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
}
]
},
// Test with coverage
{
name: "test coverage"
description: "Run tests with coverage"
implementations: [
{
script: {content: """
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out -o coverage.html
echo "Coverage report: coverage.html"
"""}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
}
]
},
// Clean command
{
name: "clean"
description: "Remove build artifacts"
implementations: [
{
script: {content: "rm -rf bin/ coverage.out coverage.html"}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}]
}
]
}
]
Salve isso como invowkfile.cue e tente:
invowk cmd
Você verá:
Available Commands
(* = default runtime)
From invowkfile:
build - Build the project [native*] (linux, macos, windows)
test unit - Run unit tests [native*, virtual-sh] (linux, macos, windows)
test coverage - Run tests with coverage [native*] (linux, macos, windows)
clean - Remove build artifacts [native*] (linux, macos)
Nomes Estilo Subcomando
Note como test unit e test coverage criam uma hierarquia. Você os executa assim:
invowk cmd test unit
invowk cmd test coverage
Isso é apenas convenção de nomenclatura - espaços no nome criam uma sensação de subcomando. É ótimo para organizar comandos relacionados!
Múltiplos Runtimes
O comando test unit permite tanto o runtime native quanto o virtual-sh:
runtimes: [{name: "native"}, {name: "virtual-sh"}]
O primeiro é o padrão. Você pode sobrescrevê-lo:
# Use the default (native)
invowk cmd test unit
# Explicitly use virtual-sh runtime
invowk cmd test unit --ivk-runtime virtual-sh
Comandos Específicos por Plataforma
O comando clean só funciona no Linux e macOS (porque usa rm -rf):
platforms: [{name: "linux"}, {name: "macos"}]
Se você tentar executá-lo no Windows, o Invowk mostrará uma mensagem de erro útil explicando que o comando não está disponível na sua plataforma.
Adicionando Dependências
Vamos tornar nosso comando de build mais inteligente verificando se o Go está instalado:
{
name: "build"
description: "Build the project"
implementations: [
{
script: {content: """
echo "Building..."
go build -o bin/app ./...
echo "Done!"
"""}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
}
]
depends_on: {
tools: [
{alternatives: ["go"]}
]
filepaths: [
{alternatives: ["go.mod"], readable: true}
]
}
}
Agora se você executar invowk cmd build sem o Go instalado, você receberá:
✗ Dependencies not satisfied!
Cannot run command 'build' because some dependencies are missing.
Missing Tools:
• go - not found in PATH
Fix the missing dependencies and try again, or update your invowkfile to remove unnecessary ones.
Variáveis de Ambiente
Você pode definir variáveis de ambiente em diferentes níveis:
// Root-level env applies to ALL commands
env: {
vars: {
GO111MODULE: "on"
}
}
cmds: [
{
name: "build"
// Command-level env applies to this command
env: {
vars: {
CGO_ENABLED: "0"
}
}
implementations: [
{
script: {content: "go build -o bin/app ./..."}
// Implementation-level env is most specific
env: {
vars: {
GOOS: "linux"
GOARCH: "amd64"
}
}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
}
]
}
]
As variáveis de ambiente são mescladas, com níveis posteriores sobrescrevendo os anteriores.
Próximos Passos
Agora você conhece o básico de criar invowkfiles! Continue aprendendo sobre:
- Conceitos Principais - Mergulho profundo no formato do invowkfile
- Modos de Runtime - Aprenda sobre runtimes native, virtual-sh, virtual-lua e container
- Dependências - Todas as formas de declarar dependências