// Copyright (c) 2026 Probo Inc . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. package add import ( "encoding/json" "fmt" "github.com/charmbracelet/huh" "github.com/spf13/cobra" "go.probo.inc/probo/pkg/cli/api" "go.probo.inc/probo/pkg/cmd/cmdutil" ) const createMutation = ` mutation($input: CreateApplicabilityStatementInput!) { createApplicabilityStatement(input: $input) { applicabilityStatementEdge { node { id applicability justification control { id sectionTitle name } } } } } ` type createResponse struct { CreateApplicabilityStatement struct { ApplicabilityStatementEdge struct { Node struct { ID string `json:"id"` Applicability bool `json:"applicability"` Justification string `json:"justification"` Control struct { ID string `json:"id"` SectionTitle string `json:"sectionTitle"` Name string `json:"name"` } `json:"control"` } `json:"node"` } `json:"applicabilityStatementEdge"` } `json:"createApplicabilityStatement"` } func NewCmdAdd(f *cmdutil.Factory) *cobra.Command { var ( flagSoA string flagControl string flagApplicable bool flagNotApplicable bool flagJustification string ) cmd := &cobra.Command{ Use: "add", Short: "Add an applicability statement to a SoA", Example: ` # Add a control as applicable prb soa statement add --soa SOA_ID --control CTRL_ID --applicable # Add a control as not applicable with justification prb soa statement add --soa SOA_ID --control CTRL_ID --not-applicable --justification "Not in scope"`, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { if flagApplicable && flagNotApplicable { return fmt.Errorf("cannot set both --applicable and --not-applicable") } if !flagApplicable && !flagNotApplicable { if !f.IOStreams.IsInteractive() { return fmt.Errorf("either --applicable or --not-applicable is required") } var choice string err := huh.NewSelect[string](). Title("Is this control applicable?"). Options( huh.NewOption("Applicable", "applicable"), huh.NewOption("Not applicable", "not-applicable"), ). Value(&choice). Run() if err != nil { return err } flagApplicable = choice == "applicable" } if !flagApplicable && flagJustification == "" && f.IOStreams.IsInteractive() { err := huh.NewText(). Title("Justification for not applicable"). Value(&flagJustification). Run() if err != nil { return err } } cfg, err := f.Config() if err != nil { return err } host, hc, err := cfg.DefaultHost() if err != nil { return err } client := api.NewClient( host, hc.Token, "/api/console/v1/graphql", cfg.HTTPTimeoutDuration(), cmdutil.TokenRefreshOption(cfg, host, hc), ) input := map[string]any{ "statementOfApplicabilityId": flagSoA, "controlId": flagControl, "applicability": flagApplicable, } if flagJustification != "" { input["justification"] = flagJustification } data, err := client.Do( createMutation, map[string]any{"input": input}, ) if err != nil { return err } var resp createResponse if err := json.Unmarshal(data, &resp); err != nil { return fmt.Errorf("cannot parse response: %w", err) } s := resp.CreateApplicabilityStatement.ApplicabilityStatementEdge.Node applicable := "not applicable" if s.Applicability { applicable = "applicable" } _, _ = fmt.Fprintf( f.IOStreams.Out, "Added statement %s: control %s (%s) marked as %s\n", s.ID, s.Control.SectionTitle, s.Control.Name, applicable, ) return nil }, } cmd.Flags().StringVar(&flagSoA, "soa", "", "Statement of applicability ID (required)") cmd.Flags().StringVar(&flagControl, "control", "", "Control ID (required)") cmd.Flags().BoolVar(&flagApplicable, "applicable", false, "Mark control as applicable") cmd.Flags().BoolVar(&flagNotApplicable, "not-applicable", false, "Mark control as not applicable") cmd.Flags().StringVar(&flagJustification, "justification", "", "Justification for the applicability decision") _ = cmd.MarkFlagRequired("soa") _ = cmd.MarkFlagRequired("control") return cmd }