Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/commoncmd/node_config_doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewCmdNodeConfigDoc() *cobra.Command {
var options CmdNodeConfigDoc
cmd := &cobra.Command{
Use: "doc",
Short: "print the documentation of the selected keywords",
Short: "print the keyword documentation",
RunE: func(cmd *cobra.Command, args []string) error {
return options.Run()
},
Expand Down
40 changes: 28 additions & 12 deletions daemon/daemonapi/lib_config_keywords.go → core/doc/main.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,61 @@
package daemonapi
package doc

import (
"errors"
"fmt"
"net/http"
"strings"

"github.com/labstack/echo/v4"

"github.com/opensvc/om3/v3/core/keywords"
"github.com/opensvc/om3/v3/core/naming"
"github.com/opensvc/om3/v3/core/xconfig"
"github.com/opensvc/om3/v3/daemon/api"
"github.com/opensvc/om3/v3/util/key"
)

func filterKeywordStore(ctx echo.Context, store keywords.Store, driver, section, option *string, path naming.Path, getConfigProvider func() (configProvider, error)) (keywords.Store, int, error) {
type (
ConfigProvider interface {
Config() *xconfig.T
}
)

var (
ErrBadRequest = errors.New("driver and section filters are mutually exclusive")
)

func FilterKeywordStore(store keywords.Store, driver, section, option *string, path naming.Path, getConfigProvider func() (ConfigProvider, error)) (keywords.Store, error) {
var err error
switch {
case driver == nil && section == nil && option == nil:
case driver != nil && section != nil && option == nil:
return nil, http.StatusBadRequest, fmt.Errorf("driver and section filters are mutually exclusive")
return nil, ErrBadRequest
case driver != nil && section == nil && option == nil:
l := keywords.ParseIndex(*driver)
store, err = store.DriverKeywords(l[0], l[1], path.Kind)
if err != nil {
return nil, http.StatusInternalServerError, err
return nil, err
}
case driver != nil && option != nil:
l := keywords.ParseIndex(*driver)
store, err = store.DriverKeywords(l[0], l[1], path.Kind)
if *option == "" && section != nil {
return store.ByOption(*section), nil
}
return store.ByOption(*option), nil
case driver == nil && section != nil && option == nil:
o, err := getConfigProvider()
if err != nil {
return nil, http.StatusInternalServerError, err
return nil, err
}
sectionType := o.Config().GetString(key.New(*section, "type"))
drvGroup, _, _ := strings.Cut(*section, "#")
store, err = store.DriverKeywords(drvGroup, sectionType, path.Kind)
if err != nil {
return nil, http.StatusInternalServerError, fmt.Errorf("%s.%s: %s", drvGroup, sectionType, err)
return nil, fmt.Errorf("%s.%s: %s", drvGroup, sectionType, err)
}
case driver == nil && section != nil && option != nil:
o, err := getConfigProvider()
if err != nil {
return nil, http.StatusInternalServerError, err
return nil, err
}
sectionType := o.Config().GetString(key.New(*section, "type"))
k := key.New(*section, *option)
Expand All @@ -50,10 +66,10 @@ func filterKeywordStore(ctx echo.Context, store keywords.Store, driver, section,
store = []keywords.Keyword{kw}
}
}
return store, http.StatusOK, nil
return store, nil
}

func convertKeywordStore(store keywords.Store) api.KeywordDefinitionItems {
func ConvertKeywordStore(store keywords.Store) api.KeywordDefinitionItems {
l := make(api.KeywordDefinitionItems, 0)
for _, kw := range store {
item := api.KeywordDefinitionItem{
Expand Down
9 changes: 9 additions & 0 deletions core/keywords/keywords.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ func (t Store) Swap(i, j int) {
t[i], t[j] = t[j], t[i]
}

func (t Store) ByOption(option string) Store {
for _, kw := range t {
if kw.Option == option {
return Store{kw}
}
}
return Store{}
}

func (t Store) Lookup(k key.T, kind naming.Kind, sectionType string) Keyword {
driverGroup := strings.Split(k.Section, "#")[0]
baseOption := k.BaseOption()
Expand Down
3 changes: 2 additions & 1 deletion core/om/kind_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package om

import (
"github.com/opensvc/om3/v3/core/commoncmd"
"github.com/opensvc/om3/v3/core/omcmd"
"github.com/opensvc/om3/v3/util/hostname"
)

Expand Down Expand Up @@ -126,7 +127,7 @@ func init() {
newCmdObjectResourceList(kind),
)
cmdObjectConfig.AddCommand(
commoncmd.NewCmdObjectConfigDoc(kind),
omcmd.NewCmdObjectConfigDoc(kind),
newCmdObjectConfigEdit(kind),
newCmdObjectConfigEval(kind),
newCmdObjectConfigGet(kind),
Expand Down
7 changes: 5 additions & 2 deletions core/om/kind_ccfg.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package om

import "github.com/opensvc/om3/v3/core/commoncmd"
import (
"github.com/opensvc/om3/v3/core/commoncmd"
"github.com/opensvc/om3/v3/core/omcmd"
)

func init() {
kind := "ccfg"
Expand Down Expand Up @@ -45,7 +48,7 @@ func init() {
newCmdObjectUnset(kind),
)
cmdObjectConfig.AddCommand(
commoncmd.NewCmdObjectConfigDoc(kind),
omcmd.NewCmdObjectConfigDoc(kind),
newCmdObjectConfigEdit(kind),
newCmdObjectConfigEval(kind),
newCmdObjectConfigGet(kind),
Expand Down
7 changes: 5 additions & 2 deletions core/om/kind_cfg.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package om

import "github.com/opensvc/om3/v3/core/commoncmd"
import (
"github.com/opensvc/om3/v3/core/commoncmd"
"github.com/opensvc/om3/v3/core/omcmd"
)

func init() {
kind := "cfg"
Expand Down Expand Up @@ -49,7 +52,7 @@ func init() {
newCmdObjectUnset(kind),
)
cmdObjectConfig.AddCommand(
commoncmd.NewCmdObjectConfigDoc(kind),
omcmd.NewCmdObjectConfigDoc(kind),
newCmdObjectConfigEdit(kind),
newCmdObjectConfigEval(kind),
newCmdObjectConfigGet(kind),
Expand Down
3 changes: 2 additions & 1 deletion core/om/kind_nscfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/spf13/cobra"

"github.com/opensvc/om3/v3/core/commoncmd"
"github.com/opensvc/om3/v3/core/omcmd"
)

func init() {
Expand Down Expand Up @@ -41,7 +42,7 @@ func init() {
)

cmdObjectConfig.AddCommand(
commoncmd.NewCmdObjectConfigDoc(kind),
omcmd.NewCmdObjectConfigDoc(kind),
newCmdObjectConfigEdit(kind),
newCmdObjectConfigEval(kind),
newCmdObjectConfigGet(kind),
Expand Down
7 changes: 5 additions & 2 deletions core/om/kind_sec.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package om

import "github.com/opensvc/om3/v3/core/commoncmd"
import (
"github.com/opensvc/om3/v3/core/commoncmd"
"github.com/opensvc/om3/v3/core/omcmd"
)

func init() {
kind := "sec"
Expand Down Expand Up @@ -59,7 +62,7 @@ func init() {
newCmdObjectCertificatePKCS(kind),
)
cmdObjectConfig.AddCommand(
commoncmd.NewCmdObjectConfigDoc(kind),
omcmd.NewCmdObjectConfigDoc(kind),
newCmdObjectConfigEdit(kind),
newCmdObjectConfigEval(kind),
newCmdObjectConfigGet(kind),
Expand Down
3 changes: 2 additions & 1 deletion core/om/kind_svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package om

import (
"github.com/opensvc/om3/v3/core/commoncmd"
"github.com/opensvc/om3/v3/core/omcmd"
"github.com/opensvc/om3/v3/util/hostname"
)

Expand Down Expand Up @@ -87,7 +88,7 @@ func init() {
newCmdObjectUnset(kind),
)
cmdObjectConfig.AddCommand(
commoncmd.NewCmdObjectConfigDoc(kind),
omcmd.NewCmdObjectConfigDoc(kind),
newCmdObjectConfigEdit(kind),
newCmdObjectConfigEval(kind),
newCmdObjectConfigGet(kind),
Expand Down
7 changes: 5 additions & 2 deletions core/om/kind_usr.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package om

import "github.com/opensvc/om3/v3/core/commoncmd"
import (
"github.com/opensvc/om3/v3/core/commoncmd"
"github.com/opensvc/om3/v3/core/omcmd"
)

func init() {
kind := "usr"
Expand Down Expand Up @@ -57,7 +60,7 @@ func init() {
newCmdObjectCertificatePKCS(kind),
)
cmdObjectConfig.AddCommand(
commoncmd.NewCmdObjectConfigDoc(kind),
omcmd.NewCmdObjectConfigDoc(kind),
newCmdObjectConfigEdit(kind),
newCmdObjectConfigEval(kind),
newCmdObjectConfigGet(kind),
Expand Down
3 changes: 2 additions & 1 deletion core/om/kind_vol.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package om

import (
"github.com/opensvc/om3/v3/core/commoncmd"
"github.com/opensvc/om3/v3/core/omcmd"
"github.com/opensvc/om3/v3/util/hostname"
)

Expand Down Expand Up @@ -88,7 +89,7 @@ func init() {
newCmdObjectCollectorTagShow(kind),
)
cmdObjectConfig.AddCommand(
commoncmd.NewCmdObjectConfigDoc(kind),
omcmd.NewCmdObjectConfigDoc(kind),
newCmdObjectConfigEdit(kind),
newCmdObjectConfigEval(kind),
newCmdObjectConfigGet(kind),
Expand Down
3 changes: 2 additions & 1 deletion core/om/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/spf13/cobra"

"github.com/opensvc/om3/v3/core/commoncmd"
"github.com/opensvc/om3/v3/core/omcmd"
)

var (
Expand Down Expand Up @@ -173,7 +174,7 @@ func init() {
newCmdNodeVersion(),
)
cmdNodeConfig.AddCommand(
commoncmd.NewCmdNodeConfigDoc(),
omcmd.NewCmdNodeConfigDoc(),
newCmdNodeConfigEdit(),
newCmdNodeConfigEval(),
newCmdNodeConfigGet(),
Expand Down
83 changes: 83 additions & 0 deletions core/omcmd/node_config_doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package omcmd

import (
"os"

"github.com/spf13/cobra"

"github.com/opensvc/om3/v3/core/commoncmd"
"github.com/opensvc/om3/v3/core/doc"
"github.com/opensvc/om3/v3/core/keywords"
"github.com/opensvc/om3/v3/core/naming"
"github.com/opensvc/om3/v3/core/object"
"github.com/opensvc/om3/v3/core/output"
"github.com/opensvc/om3/v3/core/rawconfig"
)

type (
CmdNodeConfigDoc struct {
Color string
Output string
Keyword string
Driver string
Depth int
}
)

func NewCmdNodeConfigDoc() *cobra.Command {
var options CmdNodeConfigDoc
cmd := &cobra.Command{
Use: "doc",
Short: "print the keyword documentation",
RunE: func(cmd *cobra.Command, args []string) error {
return options.Run()
},
}
flags := cmd.Flags()
commoncmd.FlagColor(flags, &options.Color)
commoncmd.FlagOutput(flags, &options.Output)
commoncmd.FlagKeyword(flags, &options.Keyword)
commoncmd.FlagDriver(flags, &options.Driver)
commoncmd.FlagDepth(flags, &options.Depth)
return cmd
}

func (t *CmdNodeConfigDoc) Run() error {
var path naming.Path
var driver, section, option *string
if t.Driver != "" {
driver = &t.Driver
}
if t.Keyword != "" {
index := keywords.ParseIndex(t.Keyword)
section = &index[0]
option = &index[1]
}
store := object.NodeKeywordStore
store, err := doc.FilterKeywordStore(store, driver, section, option, path, func() (doc.ConfigProvider, error) {
var (
i any
err error
)
i, err = object.NewNode(object.WithVolatile(true))
if err != nil {
return nil, err
}
return i.(doc.ConfigProvider), nil
})
if err != nil {
return err
}
items := doc.ConvertKeywordStore(store)
output.Renderer{
HumanRenderer: func() string {
commoncmd.Doc(os.Stdout, items, path.Kind, t.Driver, t.Keyword, t.Depth)
return ""
},
Output: t.Output,
Color: t.Color,
Data: items,
Colorize: rawconfig.Colorize,
}.Print()
return nil
}
Loading