Skip to content

Commit

Permalink
introduce compose-spec command line for compose file validation
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Oct 26, 2023
1 parent 1fc8d35 commit aa55447
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright 2020 The Compose Specification Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"fmt"
"os"

"github.com/compose-spec/compose-go/v2/cli"
)

func main() {
if len(os.Args) == 1 {
fmt.Println(`

Check failure on line 28 in cmd/main.go

View workflow job for this annotation

GitHub Actions / test (1.20, macos-latest)

printf: `fmt.Println` arg list ends with redundant newline (govet)

Check failure on line 28 in cmd/main.go

View workflow job for this annotation

GitHub Actions / test (1.19, ubuntu-latest)

printf: `fmt.Println` arg list ends with redundant newline (govet)
Validates a compose file conforms to the Compose Specification
Usage: compose-spec [OPTIONS] COMPOSE_FILE [COMPOSE_OVERRIDE_FILE]
`)
}

wd, err := os.Getwd()
if err != nil {
fmt.Errorf("can't determine current directory: %w", err)

Check failure on line 37 in cmd/main.go

View workflow job for this annotation

GitHub Actions / test (1.20, macos-latest)

unusedresult: result of fmt.Errorf call not used (govet)

Check failure on line 37 in cmd/main.go

View workflow job for this annotation

GitHub Actions / test (1.19, ubuntu-latest)

unusedresult: result of fmt.Errorf call not used (govet)
os.Exit(1)
}

options, err := cli.NewProjectOptions(os.Args[1:],
cli.WithWorkingDirectory(wd),
cli.WithOsEnv,
cli.WithDotEnv,
cli.WithConfigFileEnv,
cli.WithDefaultConfigPath,
)
if err != nil {
fmt.Errorf("failed to configure project options: %w", err)

Check failure on line 49 in cmd/main.go

View workflow job for this annotation

GitHub Actions / test (1.20, macos-latest)

unusedresult: result of fmt.Errorf call not used (govet)

Check failure on line 49 in cmd/main.go

View workflow job for this annotation

GitHub Actions / test (1.19, ubuntu-latest)

unusedresult: result of fmt.Errorf call not used (govet)
os.Exit(1)
}

project, err := cli.ProjectFromOptions(options)
if err != nil {
fmt.Errorf("failed to load project: %w", err)

Check failure on line 55 in cmd/main.go

View workflow job for this annotation

GitHub Actions / test (1.20, macos-latest)

unusedresult: result of fmt.Errorf call not used (govet)

Check failure on line 55 in cmd/main.go

View workflow job for this annotation

GitHub Actions / test (1.19, ubuntu-latest)

unusedresult: result of fmt.Errorf call not used (govet)
os.Exit(1)
}

yaml, err := project.MarshalYAML()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to marshall project: %w", err)

Check failure on line 61 in cmd/main.go

View workflow job for this annotation

GitHub Actions / test (1.20, macos-latest)

printf: fmt.Fprintf does not support error-wrapping directive %w (govet)

Check failure on line 61 in cmd/main.go

View workflow job for this annotation

GitHub Actions / test (1.19, ubuntu-latest)

printf: fmt.Fprintf does not support error-wrapping directive %w (govet)
os.Exit(1)
}
fmt.Println(string(yaml))
}

0 comments on commit aa55447

Please sign in to comment.