-
-
Notifications
You must be signed in to change notification settings - Fork 126
add pager to describe config command #1203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Benbentwo
merged 30 commits into
main
from
feature/dev-3131-add-pager-to-atmos-describe-config-command
May 13, 2025
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
2494cd1
add pager to describe config command
samtholiya 0bd38c8
add model test
samtholiya eb55512
[autofix.ci] apply automated fixes
autofix-ci[bot] 746b105
update structure
samtholiya bfa2e74
update with unit test cases
samtholiya b23ac9d
remove unwanted interface
samtholiya 03fa985
[autofix.ci] apply automated fixes
autofix-ci[bot] a20d636
fix tests
samtholiya 51bd717
updated describe config
samtholiya bb88f35
implement status with help
samtholiya b6752b9
error view added
samtholiya 5119033
[autofix.ci] apply automated fixes
autofix-ci[bot] bc455d3
fix tests
samtholiya 7ccc618
Update pkg/pager/model.go
samtholiya 1fcf428
fix lint
samtholiya aa35f38
fix golangci-lint
samtholiya fda30bb
updated pager variable
samtholiya dfc53e7
fix tests
samtholiya dd1e6da
fixed test cases
samtholiya 6b38f6b
update the pager variable logic
samtholiya 151e8da
updated the pager logic
samtholiya 29ff77a
updated test case
samtholiya e39721f
fix pager bool
samtholiya 3fa3c1d
unit test case for isPagerEnabled
samtholiya 1649fa7
fix error name
samtholiya c1de851
Merge branch 'main' into feature/dev-3131-add-pager-to-atmos-describe…
samtholiya 91b348a
Merge branch 'main' into feature/dev-3131-add-pager-to-atmos-describe…
samtholiya f4ec621
Merge branch 'main' into feature/dev-3131-add-pager-to-atmos-describe…
osterman 8feface
Merge branch 'main' into feature/dev-3131-add-pager-to-atmos-describe…
samtholiya 83e279a
Merge branch 'main' into feature/dev-3131-add-pager-to-atmos-describe…
samtholiya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 |
---|---|---|
@@ -1,51 +1,95 @@ | ||
package exec | ||
|
||
import ( | ||
u "github.com/cloudposse/atmos/pkg/utils" | ||
"github.com/spf13/cobra" | ||
"fmt" | ||
|
||
log "github.com/charmbracelet/log" | ||
"github.com/cloudposse/atmos/internal/tui/templates/term" | ||
"github.com/cloudposse/atmos/pkg/pager" | ||
"github.com/cloudposse/atmos/pkg/schema" | ||
|
||
cfg "github.com/cloudposse/atmos/pkg/config" | ||
u "github.com/cloudposse/atmos/pkg/utils" | ||
) | ||
|
||
// ExecuteDescribeConfigCmd executes `describe config` command | ||
func ExecuteDescribeConfigCmd(cmd *cobra.Command, args []string) error { | ||
flags := cmd.Flags() | ||
const describeConfigTitle = "Atmos Config" | ||
|
||
format, err := flags.GetString("format") | ||
if err != nil { | ||
return err | ||
} | ||
type DescribeConfigFormatError struct { | ||
format string | ||
} | ||
|
||
query, err := flags.GetString("query") | ||
if err != nil { | ||
return err | ||
} | ||
func (e DescribeConfigFormatError) Error() string { | ||
return fmt.Sprintf("invalid 'format': %s", e.format) | ||
} | ||
|
||
info, err := ProcessCommandLineArgs("", cmd, args, nil) | ||
if err != nil { | ||
return err | ||
} | ||
var ErrTTYNotSupported = fmt.Errorf("tty not supported for this command") | ||
|
||
atmosConfig, err := cfg.InitCliConfig(info, false) | ||
if err != nil { | ||
return err | ||
} | ||
type describeConfigExec struct { | ||
atmosConfig *schema.AtmosConfiguration | ||
pageCreator pager.PageCreator | ||
printOrWriteToFile func(format string, file string, data any) error | ||
IsTTYSupportForStdout func() bool | ||
} | ||
|
||
var res any | ||
func NewDescribeConfig(atmosConfig *schema.AtmosConfiguration) *describeConfigExec { | ||
return &describeConfigExec{ | ||
atmosConfig: atmosConfig, | ||
pageCreator: pager.New(), | ||
printOrWriteToFile: printOrWriteToFile, | ||
IsTTYSupportForStdout: term.IsTTYSupportForStdout, | ||
} | ||
} | ||
|
||
// ExecuteDescribeConfigCmd executes `describe config` command. | ||
func (d *describeConfigExec) ExecuteDescribeConfigCmd(query, format, output string) error { | ||
var res *schema.AtmosConfiguration | ||
var err error | ||
if query != "" { | ||
res, err = u.EvaluateYqExpression(&atmosConfig, atmosConfig, query) | ||
res, err = u.EvaluateYqExpressionWithType[schema.AtmosConfiguration](d.atmosConfig, *d.atmosConfig, query) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
res = atmosConfig | ||
res = d.atmosConfig | ||
} | ||
|
||
err = printOrWriteToFile(format, "", res) | ||
if err != nil { | ||
return err | ||
if d.atmosConfig.Settings.Terminal.IsPagerEnabled() { | ||
err = d.viewConfig(format, res) | ||
switch err.(type) { | ||
case DescribeConfigFormatError: | ||
return err | ||
case nil: | ||
return nil | ||
default: | ||
log.Debug("Failed to use pager") | ||
} | ||
} | ||
return d.printOrWriteToFile(format, output, res) | ||
} | ||
|
||
func (d *describeConfigExec) viewConfig(format string, data *schema.AtmosConfiguration) error { | ||
if !d.IsTTYSupportForStdout() { | ||
return ErrTTYNotSupported | ||
} | ||
var content string | ||
var err error | ||
switch format { | ||
case "yaml": | ||
content, err = u.GetAtmosConfigYAML(data) | ||
if err != nil { | ||
return err | ||
} | ||
case "json": | ||
content, err = u.GetAtmosConfigJSON(data) | ||
if err != nil { | ||
return err | ||
} | ||
default: | ||
return DescribeConfigFormatError{ | ||
format, | ||
} | ||
} | ||
if err := d.pageCreator.Run(describeConfigTitle, content); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains hidden or 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,128 @@ | ||
package exec | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cloudposse/atmos/pkg/pager" | ||
"github.com/cloudposse/atmos/pkg/schema" | ||
gomock "github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestErrInvalidFormat_Error(t *testing.T) { | ||
err := DescribeConfigFormatError{format: "invalid"} | ||
assert.Equal(t, "invalid 'format': invalid", err.Error()) | ||
} | ||
|
||
func TestDescribeConfig(t *testing.T) { | ||
// Setup test data | ||
config := &schema.AtmosConfiguration{ | ||
Components: schema.Components{ | ||
Terraform: schema.Terraform{ | ||
BasePath: "something", | ||
}, | ||
}, | ||
Settings: schema.AtmosSettings{ | ||
Terminal: schema.Terminal{ | ||
Pager: "less", | ||
}, | ||
}, | ||
} | ||
|
||
t.Run("NewDescribeConfig", func(t *testing.T) { | ||
dc := NewDescribeConfig(config) | ||
assert.Equal(t, config, dc.atmosConfig) | ||
assert.NotNil(t, dc.pageCreator) | ||
assert.NotNil(t, dc.printOrWriteToFile) | ||
}) | ||
|
||
t.Run("ExecuteDescribeConfigCmd_NoQuery_YAML_TTY", func(t *testing.T) { | ||
// Mock dependencies | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockPager := pager.NewMockPageCreator(ctrl) | ||
mockPager.EXPECT().Run(describeConfigTitle, gomock.Any()).Return(nil) | ||
dc := &describeConfigExec{ | ||
atmosConfig: config, | ||
pageCreator: mockPager, | ||
IsTTYSupportForStdout: func() bool { return true }, | ||
} | ||
|
||
err := dc.ExecuteDescribeConfigCmd("", "yaml", "") | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("ExecuteDescribeConfigCmd_NoQuery_JSON_TTY", func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
mockPager := pager.NewMockPageCreator(ctrl) | ||
mockPager.EXPECT().Run(describeConfigTitle, gomock.Any()).Return(nil) | ||
|
||
dc := &describeConfigExec{ | ||
atmosConfig: config, | ||
pageCreator: mockPager, | ||
IsTTYSupportForStdout: func() bool { return true }, | ||
} | ||
|
||
err := dc.ExecuteDescribeConfigCmd("", "json", "") | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("ExecuteDescribeConfigCmd_NoQuery_InvalidFormat_TTY", func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
dc := &describeConfigExec{ | ||
atmosConfig: config, | ||
IsTTYSupportForStdout: func() bool { return true }, | ||
} | ||
|
||
err := dc.ExecuteDescribeConfigCmd("", "invalid", "") | ||
assert.Error(t, err) | ||
assert.Equal(t, DescribeConfigFormatError{format: "invalid"}, err) | ||
}) | ||
|
||
t.Run("ExecuteDescribeConfigCmd_NoQuery_NoTTY", func(t *testing.T) { | ||
dc := &describeConfigExec{ | ||
atmosConfig: config, | ||
IsTTYSupportForStdout: func() bool { return false }, | ||
printOrWriteToFile: func(format, file string, data any) error { | ||
assert.Equal(t, "yaml", format) | ||
assert.Equal(t, "", file) | ||
assert.Equal(t, config, data) | ||
return nil | ||
}, | ||
} | ||
|
||
err := dc.ExecuteDescribeConfigCmd("", "yaml", "") | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("ExecuteDescribeConfigCmd_WithQuery", func(t *testing.T) { | ||
dc := &describeConfigExec{ | ||
atmosConfig: config, | ||
IsTTYSupportForStdout: func() bool { return false }, | ||
printOrWriteToFile: func(format, file string, data any) error { | ||
assert.Equal(t, "yaml", format) | ||
assert.Equal(t, "", file) | ||
assert.Equal(t, config, data) | ||
return nil | ||
}, | ||
} | ||
|
||
err := dc.ExecuteDescribeConfigCmd("", "yaml", "") | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("ExecuteDescribeConfigCmd_WithQuery_EvalError", func(t *testing.T) { | ||
dc := &describeConfigExec{ | ||
atmosConfig: config, | ||
IsTTYSupportForStdout: func() bool { return false }, | ||
} | ||
|
||
err := dc.ExecuteDescribeConfigCmd(".component.terraform[", "yaml", "") | ||
assert.Error(t, err) | ||
assert.Equal(t, "EvaluateYqExpressionWithType: failed to evaluate YQ expression '.component.terraform[': bad expression, could not find matching `]`", err.Error()) | ||
}) | ||
} |
This file contains hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.