-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Version-specific config tunings and custom prompt for SR Linux (#1740)
* carved out the template and added version specific config func * remove commit from the default template since commit step is executed later on * rename func * added custom prompt * fixed default config template location and added a note about unix-socket access
- Loading branch information
Showing
5 changed files
with
232 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package srl | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"regexp" | ||
"strings" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"github.com/srl-labs/containerlab/clab/exec" | ||
) | ||
|
||
func (n *srl) setCustomPrompt(tplData *srlTemplateData) { | ||
// when CLAB_CUSTOM_PROMPT is set to false, we don't generate custom prompt | ||
if strings.ToLower(os.Getenv("CLAB_CUSTOM_PROMPT")) == "false" { | ||
return | ||
} | ||
|
||
tplData.EnableCustomPrompt = true | ||
|
||
// get the current prompt | ||
prompt, err := n.currentPrompt(context.Background()) | ||
if err != nil { | ||
log.Errorf("failed to get current prompt: %v", err) | ||
tplData.EnableCustomPrompt = false | ||
return | ||
} | ||
|
||
// adding newline to the prompt for better visual separation | ||
tplData.CustomPrompt = "\\n" + prompt | ||
|
||
} | ||
|
||
// currentPrompt returns the current prompt extracted from the environment. | ||
func (n *srl) currentPrompt(ctx context.Context) (string, error) { | ||
cmd, _ := exec.NewExecCmdFromString(`sr_cli -d "environment show | grep -A 2 prompt"`) | ||
|
||
execResult, err := n.RunExec(ctx, cmd) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
log.Debugf("fetching prompt for node %s. stdout: %s, stderr: %s", n.Cfg.ShortName, execResult.GetStdOutString(), execResult.GetStdErrString()) | ||
|
||
return getPrompt(execResult.GetStdOutString()) | ||
} | ||
|
||
// getPrompt returns the prompt value from a string blob containing the prompt. | ||
// The s is the output of the "environment show | grep -A 2 prompt" command. | ||
func getPrompt(s string) (string, error) { | ||
re, _ := regexp.Compile(`value\s+=\s+"(.+)"`) | ||
v := re.FindStringSubmatch(s) | ||
|
||
if len(v) != 2 { | ||
return "", fmt.Errorf("failed to parse prompt from string: %s", s) | ||
} | ||
|
||
return v[1], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package srl | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func Test_getPrompt(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
s string | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Test with valid input", | ||
s: `value = "test-prompt"`, | ||
want: "test-prompt", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Test with invalid input", | ||
s: `invalid input`, | ||
want: "", | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := getPrompt(tt.s) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("getPrompt() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != tt.want { | ||
t.Errorf("getPrompt() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.