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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added

- Support of rocks repository specified in tt config.
- ``cfg dump`` module. It prints tt environment configuration.

## [0.3.0] - 2022-12-05

Expand Down
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,4 @@ Common description. For a detailed description, use ``tt help command`` .
* ``uninstall`` - uninstall tarantool/tt.
* ``init`` - create tt environment configuration file.
* ``daemon (experimental)`` - manage tt daemon.
* ``cfg dump`` - print tt environment configuration.
59 changes: 59 additions & 0 deletions cli/cfg/dump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cfg

import (
"fmt"
"io"
"io/ioutil"
"os"

"github.com/tarantool/tt/cli/cmdcontext"
"github.com/tarantool/tt/cli/config"
"gopkg.in/yaml.v2"
)

// DumpCtx contains information for tt config dump.
type DumpCtx struct {
// rawDump is a dump mode flag. If set, raw contents of tt configuration file is printed.
RawDump bool
}

// dumpRaw prints raw content of tt config file.
func dumpRaw(writer io.Writer, cmdCtx *cmdcontext.CmdCtx) error {
if cmdCtx.Cli.ConfigPath != "" {
_, err := os.Stat(cmdCtx.Cli.ConfigPath)
if err != nil {
return err
}
fileContent, err := ioutil.ReadFile(cmdCtx.Cli.ConfigPath)
if err != nil {
return err
}
writer.Write([]byte(cmdCtx.Cli.ConfigPath + ":\n"))
writer.Write(fileContent)
} else {
return fmt.Errorf("tt configuration file is not found")
}

return nil
}

// dumpConfiguration prints tt env configuration with all resolved paths.
func dumpConfiguration(writer io.Writer, cmdCtx *cmdcontext.CmdCtx,
cliOpts *config.CliOpts) error {
if cmdCtx.Cli.ConfigPath != "" {
if _, err := os.Stat(cmdCtx.Cli.ConfigPath); err == nil {
writer.Write([]byte(cmdCtx.Cli.ConfigPath + ":\n"))
}
}
err := yaml.NewEncoder(writer).Encode(config.Config{CliConfig: cliOpts})
return err
}

// RunDump prints tt configuration.
func RunDump(writer io.Writer, cmdCtx *cmdcontext.CmdCtx, dumpCtx *DumpCtx,
cliOpts *config.CliOpts) error {
if dumpCtx.RawDump {
return dumpRaw(writer, cmdCtx)
}
return dumpConfiguration(writer, cmdCtx, cliOpts)
}
109 changes: 109 additions & 0 deletions cli/cfg/dump_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package cfg

import (
"bytes"
"fmt"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
"github.com/tarantool/tt/cli/cmdcontext"
"github.com/tarantool/tt/cli/config"
"github.com/tarantool/tt/cli/configure"
)

func TestRunDump(t *testing.T) {
type args struct {
cmdCtx *cmdcontext.CmdCtx
dumpCtx *DumpCtx
cliOpts *config.CliOpts
}

cliOpts, configPath, err := configure.GetCliOpts("./testdata/tt_cfg.yaml")
require.NoError(t, err)
require.Equal(t, "testdata/tt_cfg.yaml", configPath)

cwd, err := os.Getwd()
require.NoError(t, err)
configDir := filepath.Join(cwd, "testdata")

tests := []struct {
name string
args args
wantWriter string
wantErr bool
}{
{
name: "Raw dump of configuration file",
args: args{
&cmdcontext.CmdCtx{
Cli: cmdcontext.CliCtx{
ConfigPath: "./testdata/tt_cfg.yaml",
},
},
&DumpCtx{RawDump: true},
configure.GetDefaultCliOpts(),
},
wantWriter: `./testdata/tt_cfg.yaml:
tt:
app:
inc_dir: ./test_inc
log_maxsize: 1024
modules:
directory: /root/modules
`,
wantErr: false,
},
{
name: "Default config dump",
args: args{
&cmdcontext.CmdCtx{
Cli: cmdcontext.CliCtx{
ConfigPath: "./testdata/tt_cfg.yaml",
},
},
&DumpCtx{RawDump: false},
cliOpts,
},
wantWriter: fmt.Sprintf(`./testdata/tt_cfg.yaml:
tt:
modules:
directory: /root/modules
app:
run_dir: %[1]s/var/run
log_dir: %[1]s/var/log
log_maxsize: 1024
log_maxage: 8
log_maxbackups: 10
restart_on_failure: false
data_dir: %[1]s/var/lib
bin_dir: %[1]s/bin
inc_dir: %[1]s/test_inc
instances_enabled: .
ee:
credential_path: ""
templates: []
repo:
rocks: ""
distfiles: %[1]s/distfiles
`, configDir),
wantErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
writer := &bytes.Buffer{}
err := RunDump(writer, tt.args.cmdCtx, tt.args.dumpCtx, tt.args.cliOpts)
if tt.wantErr {
require.Error(t, err)
return
} else {
require.NoError(t, err)
}
gotWriter := writer.String()
require.EqualValues(t, tt.wantWriter, gotWriter)
})
}
}
6 changes: 6 additions & 0 deletions cli/cfg/testdata/tt_cfg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tt:
app:
inc_dir: ./test_inc
log_maxsize: 1024
modules:
directory: /root/modules
26 changes: 26 additions & 0 deletions cli/cmd/cfg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cmd

import (
"github.com/spf13/cobra"
)

// NewCfgCmd creates a new cfg command.
func NewCfgCmd() *cobra.Command {
var cfgCmd = &cobra.Command{
Use: "cfg <command> [command flags]",
DisableFlagParsing: true,
DisableFlagsInUseLine: true,
Short: "Environment configuration management utility",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
Example: `# Print tt environment configuration:

$ tt cfg dump`,
}
cfgCmd.AddCommand(
NewDumpCmd(),
)

return cfgCmd
}
42 changes: 42 additions & 0 deletions cli/cmd/cfg_dump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cmd

import (
"os"

"github.com/spf13/cobra"
"github.com/tarantool/tt/cli/cfg"
"github.com/tarantool/tt/cli/cmdcontext"
"github.com/tarantool/tt/cli/modules"
)

var (
rawDump bool
)

// NewDumpCmd creates a new dump command.
func NewDumpCmd() *cobra.Command {
var dumpCmd = &cobra.Command{
Use: "dump",
Short: "Print environment configuration",
Run: func(cmd *cobra.Command, args []string) {
cmdCtx.CommandName = cmd.Name()
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
internalDumpModule, args)
handleCmdErr(cmd, err)
},
}

dumpCmd.Flags().BoolVarP(&rawDump, "raw", "r", false,
"Display the raw contents of tt environment config.")

return dumpCmd
}

// internalDumpModule is a default dump module.
func internalDumpModule(cmdCtx *cmdcontext.CmdCtx, args []string) error {
dumpCtx := cfg.DumpCtx{
RawDump: rawDump,
}

return cfg.RunDump(os.Stdout, cmdCtx, &dumpCtx, cliOpts)
}
1 change: 1 addition & 0 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func NewCmdRoot() *cobra.Command {
NewPackCmd(),
NewInitCmd(),
NewDaemonCmd(),
NewCfgCmd(),
)
if err := injectCmds(rootCmd); err != nil {
panic(err.Error())
Expand Down
Loading