-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathformat.go
58 lines (45 loc) · 1.71 KB
/
format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package cmd
import (
"errors"
"fmt"
"github.com/spf13/cobra"
"github.com/elastic/elastic-package/internal/cobraext"
"github.com/elastic/elastic-package/internal/formatter"
"github.com/elastic/elastic-package/pkg/packages"
)
const formatLongDescription = `Use this command to format the package files.
The formatter supports JSON and YAML format, and skips "ingest_pipeline" directories as it's hard to correctly format Handlebars template files. Formatted files are being overwritten.`
func setupFormatCommand() *cobraext.Command {
cmd := &cobra.Command{
Use: "format",
Short: "Format the package",
Long: formatLongDescription,
Args: cobra.NoArgs,
RunE: formatCommandAction,
}
cmd.Flags().BoolP(cobraext.FailFastFlagName, "f", false, cobraext.FailFastFlagDescription)
return cobraext.NewCommand(cmd, cobraext.ContextPackage)
}
func formatCommandAction(cmd *cobra.Command, args []string) error {
cmd.Println("Format the package")
packageRoot, found, err := packages.FindPackageRoot()
if err != nil {
return fmt.Errorf("locating package root failed: %w", err)
}
if !found {
return errors.New("package root not found")
}
ff, err := cmd.Flags().GetBool(cobraext.FailFastFlagName)
if err != nil {
return cobraext.FlagParsingError(err, cobraext.FailFastFlagName)
}
err = formatter.Format(packageRoot, ff)
if err != nil {
return fmt.Errorf("formatting the integration failed (path: %s, failFast: %t): %w", packageRoot, ff, err)
}
cmd.Println("Done")
return nil
}