Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
fleetctl: more cleaning and consolidate the logic to get a Unit
Browse files Browse the repository at this point in the history
  • Loading branch information
Djalal Harouni committed Feb 15, 2016
1 parent aa203d1 commit b2d34cd
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions fleetctl/fleetctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,31 @@ func getChecker() *ssh.HostKeyChecker {
return ssh.NewHostKeyChecker(keyFile)
}

func getUnit(file string) (*unit.UnitFile, error) {
var uf *unit.UnitFile

// Failing that, assume the name references a local unit file on disk,
// and attempt to load that, if it exists
if _, err := os.Stat(file); !os.IsNotExist(err) {
uf, err = getUnitFromFile(file)
if err != nil {
return nil, fmt.Errorf("failed getting Unit(%s) from file: %v", file, err)
}
} else {
// Otherwise (if the unit file does not exist), check if the name appears to be an instance unit,
// and if so, check for a corresponding template unit in the Registry
uf, err = getUnitFromTemplate(file)
if err != nil {
return nil, err
}

// If we found a template unit, create a near-identical instance unit in
// the Registry - same unit file as the template, but different name
}

return uf, nil
}

// getUnitFromFile attempts to load a Unit from a given filename
// It returns the Unit or nil, and any error encountered
func getUnitFromFile(file string) (*unit.UnitFile, error) {
Expand Down Expand Up @@ -655,24 +680,9 @@ func lazyCreateUnits(args []string) error {
continue
}

var uf *unit.UnitFile
// Failing that, assume the name references a local unit file on disk, and attempt to load that, if it exists
// TODO(mischief): consolidate these two near-identical codepaths
if _, err := os.Stat(arg); !os.IsNotExist(err) {
uf, err = getUnitFromFile(arg)
if err != nil {
return fmt.Errorf("failed getting Unit(%s) from file: %v", arg, err)
}
} else {
// Otherwise (if the unit file does not exist), check if the name appears to be an instance unit,
// and if so, check for a corresponding template unit in the Registry
uf, err = getUnitFromTemplate(name)
if err != nil {
return err
}

// If we found a template unit, create a near-identical instance unit in
// the Registry - same unit file as the template, but different name
uf, err := getUnit(arg)
if err != nil {
return err
}

_, err = createUnit(name, uf)
Expand Down

0 comments on commit b2d34cd

Please sign in to comment.