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
14 changes: 8 additions & 6 deletions command/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,19 @@ func newCompileCommand(writer io.Writer) *compileCommand {
}

func (c *compileCommand) execute(dir string) error {
inputs, err := c.configReader.ReadConfigs(dir)
ctx, err := c.configReader.ReadConfigs(dir)
if err != nil {
return err
}
var allResults []compiler.HostEntity
for _, input := range inputs {
results, err := c.compiler.Compile(input)
if err != nil {
return err
for _, s := range ctx.Sources {
for _, h := range s.Hosts {
results, err := c.compiler.Compile(h)
if err != nil {
return err
}
allResults = append(allResults, results...)
}
allResults = append(allResults, results...)
}
err = c.validator.ValidateResults(allResults)
if err != nil {
Expand Down
23 changes: 12 additions & 11 deletions command/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,32 @@ func newListCommand(writer io.Writer) *listCommand {
}

func (e *listCommand) execute(dir string) error {
files, err := e.configScanner.ScanDirectory(dir)
ctx, err := e.configReader.ReadConfigs(dir)
if err != nil {
return err
}
white := color.New(color.FgHiWhite)
for i, f := range files {
inputs, err := e.configReader.ReadConfig(f)
if err != nil {
return err
j := 0
for _, s := range ctx.Sources {
if len(s.Hosts) < 1 {
continue
}
fileDelimiter := ""
if i > 0 {
if j > 0 {
fileDelimiter = "\n"
}
_, err = white.Fprint(e.writer, fileDelimiter+f)
j++
_, err = white.Fprint(e.writer, fileDelimiter+s.SourceName)
if err != nil {
return err
}
fmt.Fprintf(e.writer, " (%d):\n", len(inputs))
for _, input := range inputs {
results, err := e.compiler.Compile(input)
fmt.Fprintf(e.writer, " (%d):\n", len(s.Hosts))
for _, h := range s.Hosts {
results, err := e.compiler.Compile(h)
if err != nil {
return err
}
_, err = white.Fprint(e.writer, "\n "+input.AliasName)
_, err = white.Fprint(e.writer, "\n "+h.AliasName)
if err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions command/test-fixtures/config_only.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
config "global" {
identity_file = "id_rsa.pem"
port = 22
}
5 changes: 0 additions & 5 deletions command/test-fixtures/consul_and_frontend.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,3 @@ host "frontend" {
alias = "front{#1}"
config = "global"
}

config "global" {
identity_file = "id_rsa.pem"
port = 22
}
11 changes: 11 additions & 0 deletions compiler/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ type ExpandingHostConfig struct {
Config ConfigProperties
}

// InputContext is the container for all host and configs
type InputContext struct {
Sources []ContextSource
}

// ContextSource is represents a single piece of source that provides host and configs definitions
type ContextSource struct {
SourceName string
Hosts []ExpandingHostConfig
}

// HostEntity is the outcome of ssh-alises compiler
type HostEntity struct {
Host string
Expand Down
73 changes: 43 additions & 30 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ import (
"github.com/dankraw/ssh-aliases/compiler"
)

type rawConfigContext struct {
type rawDirContext struct {
RawSources []rawContextSource
}

type rawContextSource struct {
SourceName string
RawContext rawFileContext
}

type rawFileContext struct {
Hosts []host `hcl:"host"`
RawConfigs rawConfigs `hcl:"config"`
}
Expand All @@ -28,22 +37,24 @@ type rawConfig []configProps

type configProps map[string]interface{}

func (c *rawConfigContext) toConfigPropertiesMap() (configPropertiesMap, error) {
func (c *rawDirContext) getConfigPropertiesMap() (configPropertiesMap, error) {
propsMap := configPropertiesMap{}
for name, r := range c.RawConfigs {
if _, exists := propsMap[name]; exists {
return configPropertiesMap{}, fmt.Errorf("duplicate config with name %v", name)
}
h := configProps{}
for _, x := range r {
for k, v := range x {
if _, ok := h[k]; ok {
return configPropertiesMap{}, fmt.Errorf("duplicate config entry `%v` in host `%v`", k, name)
for _, s := range c.RawSources {
for name, r := range s.RawContext.RawConfigs {
if _, exists := propsMap[name]; exists {
return configPropertiesMap{}, fmt.Errorf("duplicate config with name `%v`", name)
}
h := configProps{}
for _, x := range r {
for k, v := range x {
if _, ok := h[k]; ok {
return configPropertiesMap{}, fmt.Errorf("duplicate config entry `%v` in host `%v`", k, name)
}
h[k] = v
}
h[k] = v
}
propsMap[name] = h.toSortedProperties()
}
propsMap[name] = h.toSortedProperties()
}
return propsMap, nil
}
Expand All @@ -61,11 +72,8 @@ func (c *configProps) toSortedProperties() compiler.ConfigProperties {
return entries
}

func (c *rawConfigContext) toExpandingHostConfigs() ([]compiler.ExpandingHostConfig, error) {
configsMap, err := c.toConfigPropertiesMap()
if err != nil {
return nil, err
}
func (c *rawFileContext) toExpandingHostConfigs(propsMap *configPropertiesMap) ([]compiler.ExpandingHostConfig, error) {
configsMap := *propsMap
var inputs []compiler.ExpandingHostConfig

aliases := map[string]host{}
Expand Down Expand Up @@ -106,19 +114,24 @@ func (c *rawConfigContext) toExpandingHostConfigs() ([]compiler.ExpandingHostCon
return inputs, nil
}

func mergeRawConfigContexts(contexts ...rawConfigContext) (rawConfigContext, error) {
m := rawConfigContext{
Hosts: []host{},
RawConfigs: rawConfigs{},
func (c *rawDirContext) toCompilerInputContext() (compiler.InputContext, error) {
propsMap, err := c.getConfigPropertiesMap()
if err != nil {
return compiler.InputContext{}, err
}
for _, c := range contexts {
m.Hosts = append(m.Hosts, c.Hosts...)
for n, r := range c.RawConfigs {
if _, ok := m.RawConfigs[n]; ok {
return rawConfigContext{}, fmt.Errorf("duplicate config `%s`", n)
}
m.RawConfigs[n] = r
var sources []compiler.ContextSource
for _, s := range c.RawSources {
expandingHostConfigs, err := s.RawContext.toExpandingHostConfigs(&propsMap)
if err != nil {
return compiler.InputContext{}, err
}
ctxSource := compiler.ContextSource{
SourceName: s.SourceName,
Hosts: expandingHostConfigs,
}
sources = append(sources, ctxSource)
}
return m, nil
return compiler.InputContext{
Sources: sources,
}, nil
}
Loading