Skip to content

Commit

Permalink
remove fat
Browse files Browse the repository at this point in the history
  • Loading branch information
codemodify committed Jun 1, 2020
1 parent c09f241 commit 8961441
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 76 deletions.
11 changes: 4 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ module github.com/codemodify/systemkit-service
go 1.13

require (
github.com/codemodify/systemkit-helpers-conv v1.7.9
github.com/codemodify/systemkit-helpers-os v1.7.9
github.com/codemodify/systemkit-helpers-reflection v1.7.9
github.com/codemodify/systemkit-logging v1.8.4
github.com/codemodify/systemkit-service-encoders-launchd v1.0.0
github.com/codemodify/systemkit-service-encoders-rc_d v1.0.0
github.com/codemodify/systemkit-service-encoders-systemd v1.0.0
github.com/codemodify/systemkit-service-encoders-systemv v1.0.0
github.com/codemodify/systemkit-service-encoders-upstart v1.0.0
github.com/codemodify/systemkit-service-encoders-rc_d v1.0.1
github.com/codemodify/systemkit-service-encoders-systemd v1.0.2
github.com/codemodify/systemkit-service-encoders-systemv v1.0.1
github.com/codemodify/systemkit-service-encoders-upstart v1.0.1
github.com/codemodify/systemkit-service-spec v1.0.1
golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9
)
22 changes: 8 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
github.com/codemodify/systemkit-helpers-conv v1.7.9 h1:a3MRS88pBnOauCX82xuZEnHeJSfULQm6mVl9mIfC23Q=
github.com/codemodify/systemkit-helpers-conv v1.7.9/go.mod h1:yqHcvZcVmd9RPIIi49biNO9ALSY3CFTQeUnOVc1GjDY=
github.com/codemodify/systemkit-helpers-os v1.7.9 h1:Z1EgPCkhj/zoZ9sKlgsx1lLw/eRvwU2h5mMWGvvyyQU=
github.com/codemodify/systemkit-helpers-os v1.7.9/go.mod h1:2j8txlmkBgd4jjTz5h2nAvRPeK2D5ibE2i2g3wrOnLk=
github.com/codemodify/systemkit-helpers-reflection v1.7.9 h1:vPsYvbOiEZHT+rpoXlRx14IvzsKEAkYy7Aw2QEjF/G0=
github.com/codemodify/systemkit-helpers-reflection v1.7.9/go.mod h1:HunZSrygIcl5IBmnWAYoDoqvFRVu7GIUvC0/scCfPzY=
github.com/codemodify/systemkit-logging v1.8.4 h1:nkbj0VpnRnpI4PE4w9HNUv/ySO3A/1Y9LVeT9j+ZvGM=
github.com/codemodify/systemkit-logging v1.8.4/go.mod h1:29Zc9P/MnY2bgenJU9aoDszUef+Xf40b3ajtusTBC1Q=
github.com/codemodify/systemkit-service-encoders-launchd v1.0.0 h1:S9cgQPK9hkt+cp6Uwe0Eb3zSf7arNBqu/Ls06o43Dcw=
github.com/codemodify/systemkit-service-encoders-launchd v1.0.0/go.mod h1:Tylr2+OiLYhn56y8MRvlbl3WfT1rTXPtJEZGiP/1FWk=
github.com/codemodify/systemkit-service-encoders-rc_d v1.0.0 h1:r+JaDxsrcbBjyGT8hRFol5YcCZNvT46dEF2AD1epFjI=
github.com/codemodify/systemkit-service-encoders-rc_d v1.0.0/go.mod h1:itEzE535yOXIReXz1DR8sliY5POhHQmrZ4aC4Ad1tEk=
github.com/codemodify/systemkit-service-encoders-systemd v1.0.0 h1:D6sInl0a7Lnz8NeRyHYVoYD17Zd7JdfalAvR0zySrd8=
github.com/codemodify/systemkit-service-encoders-systemd v1.0.0/go.mod h1:Dt0AUUz+4AstT3MJTd0l7Vpgx+Vpo/WGLNF5LaAt63o=
github.com/codemodify/systemkit-service-encoders-systemv v1.0.0 h1:/ZDlCdK9K4yviWKtNseTaMFdht/yQ/ZdRwhcVyHSGUI=
github.com/codemodify/systemkit-service-encoders-systemv v1.0.0/go.mod h1:BwZ2zjABnOxflGeaLJ0qojSFyeq80VDQgLXUOIXX36c=
github.com/codemodify/systemkit-service-encoders-upstart v1.0.0 h1:/63fIbsFDCIQAiY0NNiDfcB+Zh31mxxzY9KVg2ubgCU=
github.com/codemodify/systemkit-service-encoders-upstart v1.0.0/go.mod h1:Oi5kzF810agTz/DJOL+v2uhaF9oC+sqkEd1WhmZCg70=
github.com/codemodify/systemkit-service-encoders-rc_d v1.0.1 h1:6FpnxwhaH4LKk3a2FMGtGYI/2D0lwSzvubfptid9UE0=
github.com/codemodify/systemkit-service-encoders-rc_d v1.0.1/go.mod h1:2BkmLV28VQH66vETGqr4tl07YxLmomaH5qLln3AHefI=
github.com/codemodify/systemkit-service-encoders-systemd v1.0.2 h1:KBvunQ7fQBtHbwjKQKBUweiv/aTyEVObkqMEOtbufhk=
github.com/codemodify/systemkit-service-encoders-systemd v1.0.2/go.mod h1:mOjsWZXg3wF9IEcovq5JCfr4aO98Ct4/AEmsmKtPpoc=
github.com/codemodify/systemkit-service-encoders-systemv v1.0.1 h1:FZ4B4abQ3C9shxUmg56ahEOzfclIBVCdVqnJWaVzn+I=
github.com/codemodify/systemkit-service-encoders-systemv v1.0.1/go.mod h1:snmOHK9ByvPn03jukY0ixjUokmgG9veSLNIzNBuaBKM=
github.com/codemodify/systemkit-service-encoders-upstart v1.0.1 h1:oN9959zaKCfEnvPuKln3aAsU4cqRydFDI7vqrTgf7Fw=
github.com/codemodify/systemkit-service-encoders-upstart v1.0.1/go.mod h1:BcJD0iLp++xhQQTSeYo5qoIa5r5eerC8igwe7PsxPyk=
github.com/codemodify/systemkit-service-spec v1.0.1 h1:UHg60PsR+2blb9kqE3j+pJbHhUys6j+mQ8QprxrytBg=
github.com/codemodify/systemkit-service-spec v1.0.1/go.mod h1:/GURRGinl4NxbiK+mi9RIkrsXhNkjfIaIOma5SWF4nM=
github.com/groob/plist v0.0.0-20200425180238-0f631f258c01 h1:0T3XGXebqLj7zSVLng9wX9axQzTEnvj/h6eT7iLfUas=
Expand Down
55 changes: 55 additions & 0 deletions helpers/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package helpers

import (
"encoding/json"
"fmt"
"os/exec"
"os/user"
)

func AsJSONString(i interface{}) string {
bytes, err := json.Marshal(i)
if err != nil {
return fmt.Sprintf("ERROR: AsJSONString(), details [%s]", err.Error())
}
return string(bytes)
}

func Is(err1 error, err2 error) bool {
if err1 == err2 {
return true
}

if err1 != nil && err2 != nil {
return err1.Error() == err2.Error()
}

return false
}

func ExecWithArgs(name string, args ...string) (out string, err error) {
output, err := exec.Command(name, args...).CombinedOutput()
return string(output), err
}


func IsRoot() bool {
u, err := user.Current()

if err != nil {
return false
}

// On unix systems, root user either has the UID 0,
// the GID 0 or both.
return u.Uid == "0" || u.Gid == "0"
}

func HomeDir(returnIfError string) string {
u, err := user.Current()
if err != nil {
return returnIfError
}

return u.HomeDir
}
25 changes: 11 additions & 14 deletions service_darwin-launchd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ import (
"strconv"
"strings"

helpersJSON "github.com/codemodify/systemkit-helpers-conv"
helpersExec "github.com/codemodify/systemkit-helpers-os"
helpersUser "github.com/codemodify/systemkit-helpers-os"
helpersErrors "github.com/codemodify/systemkit-helpers-reflection"
logging "github.com/codemodify/systemkit-logging"
encoders "github.com/codemodify/systemkit-service-encoders-launchd"
spec "github.com/codemodify/systemkit-service-spec"
"github.com/codemodify/systemkit-service/helpers"
)

var logTag = "LaunchD-SERVICE"
Expand All @@ -29,8 +26,8 @@ type launchdService struct {
func newServiceFromSERVICE(serviceSpec spec.SERVICE) Service {
// override some values - platform specific
// https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html
logDir := filepath.Join(helpersUser.HomeDir(""), "Library/Logs", serviceSpec.Name)
if helpersUser.IsRoot() {
logDir := filepath.Join(helpers.HomeDir(""), "Library/Logs", serviceSpec.Name)
if helpers.IsRoot() {
logDir = filepath.Join("/Library/Logs", serviceSpec.Name)
}

Expand All @@ -42,7 +39,7 @@ func newServiceFromSERVICE(serviceSpec spec.SERVICE) Service {
serviceSpec.Logging.StdErr.Value = filepath.Join(logDir, serviceSpec.Name+".stderr.log")
}

logging.Debugf("%s: serviceSpec object: %s", logTag, helpersJSON.AsJSONString(serviceSpec))
logging.Debugf("%s: serviceSpec object: %s", logTag, helpers.AsJSONString(serviceSpec))

launchdService := &launchdService{
serviceSpec: serviceSpec,
Expand All @@ -53,8 +50,8 @@ func newServiceFromSERVICE(serviceSpec spec.SERVICE) Service {
}

func newServiceFromName(name string) (Service, error) {
serviceFile := filepath.Join(helpersUser.HomeDir(""), "Library/LaunchAgents", name+".plist")
if helpersUser.IsRoot() {
serviceFile := filepath.Join(helpers.HomeDir(""), "Library/LaunchAgents", name+".plist")
if helpers.IsRoot() {
serviceFile = filepath.Join("/Library/LaunchDaemons", name+".plist")
}

Expand Down Expand Up @@ -101,7 +98,7 @@ func (thisRef launchdService) Install() error {
func (thisRef launchdService) Uninstall() error {
// 1.
err := thisRef.Stop()
if err != nil && !helpersErrors.Is(err, ErrServiceDoesNotExist) {
if err != nil && !helpers.Is(err, ErrServiceDoesNotExist) {
return err
}

Expand Down Expand Up @@ -197,21 +194,21 @@ func (thisRef launchdService) Info() Info {
}

func (thisRef launchdService) filePath() string {
if helpersUser.IsRoot() {
if helpers.IsRoot() {
return filepath.Join("/Library/LaunchDaemons", thisRef.serviceSpec.Name+".plist")
}

return filepath.Join(helpersUser.HomeDir(""), "Library/LaunchAgents", thisRef.serviceSpec.Name+".plist")
return filepath.Join(helpers.HomeDir(""), "Library/LaunchAgents", thisRef.serviceSpec.Name+".plist")
}

func runLaunchCtlCommand(args ...string) (string, error) {
// if !helpersUser.IsRoot() {
// if !helpers.IsRoot() {
// args = append([]string{"--user"}, args...)
// }

logging.Debugf("%s: RUN-LAUNCHCTL: launchctl %s", logTag, strings.Join(args, " "))

output, err := helpersExec.ExecWithArgs("launchctl", args...)
output, err := helpers.ExecWithArgs("launchctl", args...)
errAsString := ""
if err != nil {
errAsString = err.Error()
Expand Down
10 changes: 4 additions & 6 deletions service_freebsd-rc-d.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ import (
"path/filepath"
"strings"

helpersJSON "github.com/codemodify/systemkit-helpers-conv"
helpersExec "github.com/codemodify/systemkit-helpers-os"
helpersErrors "github.com/codemodify/systemkit-helpers-reflection"
logging "github.com/codemodify/systemkit-logging"
encoders "github.com/codemodify/systemkit-service-encoders-rc_d"
spec "github.com/codemodify/systemkit-service-spec"
"github.com/codemodify/systemkit-service/helpers"
)

var logTagRCD = "rc.d-SERVICE"
Expand All @@ -25,7 +23,7 @@ type rcdService struct {
}

func newServiceFromSERVICE(serviceSpec spec.SERVICE) Service {
logging.Debugf("%s: serviceSpec object: %s", logTagRCD, helpersJSON.AsJSONString(serviceSpec))
logging.Debugf("%s: serviceSpec object: %s", logTagRCD, helpers.AsJSONString(serviceSpec))

return &rcdService{
serviceSpec: serviceSpec,
Expand Down Expand Up @@ -105,7 +103,7 @@ func (thisRef rcdService) Uninstall() error {

// 2.
err := thisRef.Stop()
if err != nil && !helpersErrors.Is(err, ErrServiceDoesNotExist) {
if err != nil && !helpers.Is(err, ErrServiceDoesNotExist) {
return err
}

Expand Down Expand Up @@ -197,7 +195,7 @@ func (thisRef rcdService) filePath() string {
func runServiceCommand(args ...string) (string, error) {
logging.Debugf("%s: RUN-SERVICE: service %s", logTagRCD, strings.Join(args, " "))

output, err := helpersExec.ExecWithArgs("service", args...)
output, err := helpers.ExecWithArgs("service", args...)
errAsString := ""
if err != nil {
errAsString = err.Error()
Expand Down
21 changes: 9 additions & 12 deletions service_linux-systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ import (
"strconv"
"strings"

helpersJSON "github.com/codemodify/systemkit-helpers-conv"
helpersExec "github.com/codemodify/systemkit-helpers-os"
helpersUser "github.com/codemodify/systemkit-helpers-os"
helpersErrors "github.com/codemodify/systemkit-helpers-reflection"
logging "github.com/codemodify/systemkit-logging"
encoders "github.com/codemodify/systemkit-service-encoders-systemd"
spec "github.com/codemodify/systemkit-service-spec"
"github.com/codemodify/systemkit-service/helpers"
)

var logTagSystemD = "SystemD-SERVICE"
Expand All @@ -27,7 +24,7 @@ type systemdService struct {
}

func newServiceFromSERVICE_SystemD(serviceSpec spec.SERVICE) Service {
logging.Debugf("%s: spec.SERVICE object: %s", logTagSystemD, helpersJSON.AsJSONString(serviceSpec))
logging.Debugf("%s: spec.SERVICE object: %s", logTagSystemD, helpers.AsJSONString(serviceSpec))

return &systemdService{
serviceSpec: serviceSpec,
Expand All @@ -39,7 +36,7 @@ func newServiceFromName_SystemD(name string) (Service, error) {
fileContent := []byte{}
var err error

if helpersUser.IsRoot() {
if helpers.IsRoot() {
serviceFile := filepath.Join("/etc/systemd/system", name+".service")
fileContent, err = ioutil.ReadFile(serviceFile)
if err != nil {
Expand All @@ -50,7 +47,7 @@ func newServiceFromName_SystemD(name string) (Service, error) {
}
}
} else {
serviceFile := filepath.Join(helpersUser.HomeDir(""), ".config/systemd/user", name+".service")
serviceFile := filepath.Join(helpers.HomeDir(""), ".config/systemd/user", name+".service")
fileContent, err = ioutil.ReadFile(serviceFile)
if err != nil {
return nil, ErrServiceDoesNotExist
Expand Down Expand Up @@ -106,7 +103,7 @@ func (thisRef systemdService) Uninstall() error {

// 2.
err := thisRef.Stop()
if err != nil && !helpersErrors.Is(err, ErrServiceDoesNotExist) {
if err != nil && !helpers.Is(err, ErrServiceDoesNotExist) {
return err
}

Expand Down Expand Up @@ -246,21 +243,21 @@ func (thisRef systemdService) Info() Info {
}

func (thisRef systemdService) filePath() string {
if helpersUser.IsRoot() {
if helpers.IsRoot() {
return filepath.Join("/etc/systemd/system", thisRef.serviceSpec.Name+".service")
}

return filepath.Join(helpersUser.HomeDir(""), ".config/systemd/user", thisRef.serviceSpec.Name+".service")
return filepath.Join(helpers.HomeDir(""), ".config/systemd/user", thisRef.serviceSpec.Name+".service")
}

func runSystemCtlCommand(args ...string) (string, error) {
if !helpersUser.IsRoot() {
if !helpers.IsRoot() {
args = append([]string{"--user"}, args...)
}

logging.Debugf("%s: RUN-SYSTEMCTL: systemctl %s", logTagSystemD, strings.Join(args, " "))

output, err := helpersExec.ExecWithArgs("systemctl", args...)
output, err := helpers.ExecWithArgs("systemctl", args...)
errAsString := ""
if err != nil {
errAsString = err.Error()
Expand Down
13 changes: 5 additions & 8 deletions service_linux-systemv.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ import (
"path/filepath"
"strings"

helpersJSON "github.com/codemodify/systemkit-helpers-conv"
helpersExec "github.com/codemodify/systemkit-helpers-os"
helpersUser "github.com/codemodify/systemkit-helpers-os"
helpersErrors "github.com/codemodify/systemkit-helpers-reflection"
logging "github.com/codemodify/systemkit-logging"
encoders "github.com/codemodify/systemkit-service-encoders-systemv"
spec "github.com/codemodify/systemkit-service-spec"
"github.com/codemodify/systemkit-service/helpers"
)

var logTagSystemV = "SystemV-SERVICE"
Expand All @@ -26,7 +23,7 @@ type systemvService struct {
}

func newServiceFromSERVICE_SystemV(serviceSpec spec.SERVICE) Service {
logging.Debugf("%s: serviceSpec object: %s", logTagSystemV, helpersJSON.AsJSONString(serviceSpec))
logging.Debugf("%s: serviceSpec object: %s", logTagSystemV, helpers.AsJSONString(serviceSpec))

return &systemvService{
serviceSpec: serviceSpec,
Expand Down Expand Up @@ -103,7 +100,7 @@ func (thisRef systemvService) Uninstall() error {

// 2.
err := thisRef.Stop()
if err != nil && !helpersErrors.Is(err, ErrServiceDoesNotExist) {
if err != nil && !helpers.Is(err, ErrServiceDoesNotExist) {
return err
}

Expand Down Expand Up @@ -193,13 +190,13 @@ func (thisRef systemvService) filePath() string {
}

func runServiceCommand(args ...string) (string, error) {
if !helpersUser.IsRoot() {
if !helpers.IsRoot() {
args = append([]string{"--user"}, args...)
}

logging.Debugf("%s: RUN-SERVICE: service %s", logTagSystemV, strings.Join(args, " "))

output, err := helpersExec.ExecWithArgs("service", args...)
output, err := helpers.ExecWithArgs("service", args...)
errAsString := ""
if err != nil {
errAsString = err.Error()
Expand Down
Loading

0 comments on commit 8961441

Please sign in to comment.