MCPcopy
hub / github.com/grafana/tempo / newCmd

Function newCmd

tools/chloggen/cmd/new.go:29–74  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

27var filename string
28
29func newCmd() *cobra.Command {
30 cmd := &cobra.Command{
31 Use: "new",
32 Short: "Creates new change file",
33 RunE: func(cmd *cobra.Command, _ []string) error {
34 sanitizedName := filepath.Base(cleanFileName(filename))
35 if sanitizedName == "." || sanitizedName == ".." || sanitizedName == "" {
36 return fmt.Errorf("invalid filename %q", filename)
37 }
38
39 path := filepath.Join(globalCfg.EntriesDir, sanitizedName)
40 var pathWithExt string
41 switch ext := filepath.Ext(path); ext {
42 case ".yaml":
43 pathWithExt = path
44 case ".yml":
45 pathWithExt = strings.TrimSuffix(path, ".yml") + ".yaml"
46 default:
47 pathWithExt = path + ".yaml"
48 }
49
50 templateFile, err := os.Open(filepath.Clean(globalCfg.TemplateYAML))
51 if err != nil {
52 return err
53 }
54 defer templateFile.Close()
55
56 templateBytes, err := io.ReadAll(templateFile)
57 if err != nil {
58 return err
59 }
60 err = os.WriteFile(filepath.Clean(pathWithExt), templateBytes, os.FileMode(0o644))
61 if err != nil {
62 return err
63 }
64 cmd.Printf("Changelog entry template copied to: %s\n", pathWithExt)
65 return nil
66 },
67 }
68 cmd.Flags().StringVarP(&filename, "filename", "f", "", "name of the file to add")
69 if err := cmd.MarkFlagRequired("filename"); err != nil {
70 cmd.PrintErrf("could not mark filename flag as required: %v", err)
71 os.Exit(1)
72 }
73 return cmd
74}
75
76func cleanFileName(filename string) string {
77 replace := strings.NewReplacer("/", "_", "\\", "_")

Callers 1

rootCmdFunction · 0.85

Calls 4

cleanFileNameFunction · 0.85
JoinMethod · 0.65
CloseMethod · 0.65
ReadAllMethod · 0.65

Tested by

no test coverage detected