-
Notifications
You must be signed in to change notification settings - Fork 9
/
pkg.go
57 lines (47 loc) · 1.5 KB
/
pkg.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package create
import (
"context"
"fmt"
"github.com/spf13/cobra"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/console/colors"
"namespacelabs.dev/foundation/internal/fnfs"
"namespacelabs.dev/foundation/internal/parsing"
"namespacelabs.dev/foundation/std/cfg"
)
type targetPkg struct {
Location fnfs.Location
Root *parsing.Root
}
func parseTargetPkgWithDeps(targetPkgOut *targetPkg, typ string) []fncobra.ArgsParser {
var (
env cfg.Context
locs fncobra.Locations
)
return []fncobra.ArgsParser{
fncobra.ParseEnv(&env),
fncobra.ParseLocations(&locs, &env, fncobra.ParseLocationsOpts{RequireSingle: true}),
&targetPkgParser{targetPkgOut, &locs, typ},
}
}
type targetPkgParser struct {
targetPkgOut *targetPkg
locs *fncobra.Locations
typ string
}
func (p *targetPkgParser) AddFlags(cmd *cobra.Command) {}
func (p *targetPkgParser) Parse(ctx context.Context, args []string) error {
loc := p.locs.Locations[0]
if loc.RelPath == "." {
cmd := fmt.Sprintf("ns create %s", p.typ)
return fmt.Errorf(
"cannot create %s at workspace root. Please specify %s location or run %s at the target directory",
p.typ, p.typ, colors.Ctx(ctx).Highlight.Apply(cmd))
}
p.targetPkgOut.Location = loc
p.targetPkgOut.Root = p.locs.Root
return nil
}