Skip to content

Commit

Permalink
Merge branch 'release/0.3.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
takama committed Mar 11, 2016
2 parents 9bef40d + ce61f46 commit 552d5ed
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 105 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 Igor Dolzhikov
Copyright (c) 2016 Igor Dolzhikov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 2 additions & 2 deletions daemon.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright 2015 Igor Dolzhikov. All rights reserved.
// Copyright 2016 Igor Dolzhikov. All rights reserved.
// Use of this source code is governed by
// license that can be found in the LICENSE file.

/*
Package daemon 0.3.0 for use with Go (golang) services.
Package daemon 0.3.1 for use with Go (golang) services.
Package daemon provides primitives for daemonization of golang services.
This package is not provide implementation of user daemon,
Expand Down
55 changes: 27 additions & 28 deletions daemon_darwin.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Copyright 2015 Igor Dolzhikov. All rights reserved.
// Copyright 2016 Igor Dolzhikov. All rights reserved.
// Use of this source code is governed by
// license that can be found in the LICENSE file.

// Package daemon darwin (mac os x) version
package daemon

import (
"errors"
"os"
"os/exec"
"path/filepath"
Expand All @@ -31,8 +30,8 @@ func (darwin *darwinRecord) servicePath() string {
return "/Library/LaunchDaemons/" + darwin.name + ".plist"
}

// Check service is installed
func (darwin *darwinRecord) checkInstalled() bool {
// Is a service installed
func (darwin *darwinRecord) isInstalled() bool {

if _, err := os.Stat(darwin.servicePath()); err == nil {
return true
Expand Down Expand Up @@ -67,14 +66,14 @@ func (darwin *darwinRecord) checkRunning() (string, bool) {
func (darwin *darwinRecord) Install() (string, error) {
installAction := "Install " + darwin.description + ":"

if checkPrivileges() == false {
return installAction + failed, errors.New(rootPrivileges)
if ok, err := checkPrivileges(); !ok {
return installAction + failed, err
}

srvPath := darwin.servicePath()

if darwin.checkInstalled() == true {
return installAction + failed, errors.New(darwin.description + " already installed")
if darwin.isInstalled() {
return installAction + failed, ErrAlreadyInstalled
}

file, err := os.Create(srvPath)
Expand Down Expand Up @@ -109,12 +108,12 @@ func (darwin *darwinRecord) Install() (string, error) {
func (darwin *darwinRecord) Remove() (string, error) {
removeAction := "Removing " + darwin.description + ":"

if checkPrivileges() == false {
return removeAction + failed, errors.New(rootPrivileges)
if ok, err := checkPrivileges(); !ok {
return removeAction + failed, err
}

if darwin.checkInstalled() == false {
return removeAction + failed, errors.New(darwin.description + " is not installed")
if !darwin.isInstalled() {
return removeAction + failed, ErrNotInstalled
}

if err := os.Remove(darwin.servicePath()); err != nil {
Expand All @@ -128,16 +127,16 @@ func (darwin *darwinRecord) Remove() (string, error) {
func (darwin *darwinRecord) Start() (string, error) {
startAction := "Starting " + darwin.description + ":"

if checkPrivileges() == false {
return startAction + failed, errors.New(rootPrivileges)
if ok, err := checkPrivileges(); !ok {
return startAction + failed, err
}

if darwin.checkInstalled() == false {
return startAction + failed, errors.New(darwin.description + " is not installed")
if !darwin.isInstalled() {
return startAction + failed, ErrNotInstalled
}

if _, status := darwin.checkRunning(); status == true {
return startAction + failed, errors.New("service already running")
if _, ok := darwin.checkRunning(); ok {
return startAction + failed, ErrAlreadyRunning
}

if err := exec.Command("launchctl", "load", darwin.servicePath()).Run(); err != nil {
Expand All @@ -151,16 +150,16 @@ func (darwin *darwinRecord) Start() (string, error) {
func (darwin *darwinRecord) Stop() (string, error) {
stopAction := "Stopping " + darwin.description + ":"

if checkPrivileges() == false {
return stopAction + failed, errors.New(rootPrivileges)
if ok, err := checkPrivileges(); !ok {
return stopAction + failed, err
}

if darwin.checkInstalled() == false {
return stopAction + failed, errors.New(darwin.description + " is not installed")
if !darwin.isInstalled() {
return stopAction + failed, ErrNotInstalled
}

if _, status := darwin.checkRunning(); status == false {
return stopAction + failed, errors.New("service already stopped")
if _, ok := darwin.checkRunning(); !ok {
return stopAction + failed, ErrAlreadyStopped
}

if err := exec.Command("launchctl", "unload", darwin.servicePath()).Run(); err != nil {
Expand All @@ -173,12 +172,12 @@ func (darwin *darwinRecord) Stop() (string, error) {
// Status - Get service status
func (darwin *darwinRecord) Status() (string, error) {

if checkPrivileges() == false {
return "", errors.New(rootPrivileges)
if ok, err := checkPrivileges(); !ok {
return "", err
}

if darwin.checkInstalled() == false {
return "Status could not defined", errors.New(darwin.description + " is not installed")
if !darwin.isInstalled() {
return "Status could not defined", ErrNotInstalled
}

statusAction, _ := darwin.checkRunning()
Expand Down
2 changes: 1 addition & 1 deletion daemon_linux.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015 Igor Dolzhikov. All rights reserved.
// Copyright 2016 Igor Dolzhikov. All rights reserved.
// Use of this source code is governed by
// license that can be found in the LICENSE file.

Expand Down
55 changes: 27 additions & 28 deletions daemon_linux_systemd.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// Copyright 2015 Igor Dolzhikov. All rights reserved.
// Copyright 2016 Igor Dolzhikov. All rights reserved.
// Use of this source code is governed by
// license that can be found in the LICENSE file.

package daemon

import (
"errors"
"os"
"os/exec"
"regexp"
Expand All @@ -25,8 +24,8 @@ func (linux *systemDRecord) servicePath() string {
return "/etc/systemd/system/" + linux.name + ".service"
}

// Check service is installed
func (linux *systemDRecord) checkInstalled() bool {
// Is a service installed
func (linux *systemDRecord) isInstalled() bool {

if _, err := os.Stat(linux.servicePath()); err == nil {
return true
Expand Down Expand Up @@ -56,14 +55,14 @@ func (linux *systemDRecord) checkRunning() (string, bool) {
func (linux *systemDRecord) Install() (string, error) {
installAction := "Install " + linux.description + ":"

if checkPrivileges() == false {
return installAction + failed, errors.New(rootPrivileges)
if ok, err := checkPrivileges(); !ok {
return installAction + failed, err
}

srvPath := linux.servicePath()

if linux.checkInstalled() == true {
return installAction + failed, errors.New(linux.description + " already installed")
if linux.isInstalled() {
return installAction + failed, ErrAlreadyInstalled
}

file, err := os.Create(srvPath)
Expand Down Expand Up @@ -106,12 +105,12 @@ func (linux *systemDRecord) Install() (string, error) {
func (linux *systemDRecord) Remove() (string, error) {
removeAction := "Removing " + linux.description + ":"

if checkPrivileges() == false {
return removeAction + failed, errors.New(rootPrivileges)
if ok, err := checkPrivileges(); !ok {
return removeAction + failed, err
}

if linux.checkInstalled() == false {
return removeAction + failed, errors.New(linux.description + " is not installed")
if !linux.isInstalled() {
return removeAction + failed, ErrNotInstalled
}

if err := exec.Command("systemctl", "disable", linux.name+".service").Run(); err != nil {
Expand All @@ -129,16 +128,16 @@ func (linux *systemDRecord) Remove() (string, error) {
func (linux *systemDRecord) Start() (string, error) {
startAction := "Starting " + linux.description + ":"

if checkPrivileges() == false {
return startAction + failed, errors.New(rootPrivileges)
if ok, err := checkPrivileges(); !ok {
return startAction + failed, err
}

if linux.checkInstalled() == false {
return startAction + failed, errors.New(linux.description + " is not installed")
if !linux.isInstalled() {
return startAction + failed, ErrNotInstalled
}

if _, status := linux.checkRunning(); status == true {
return startAction + failed, errors.New("service already running")
if _, ok := linux.checkRunning(); ok {
return startAction + failed, ErrAlreadyRunning
}

if err := exec.Command("systemctl", "start", linux.name+".service").Run(); err != nil {
Expand All @@ -152,16 +151,16 @@ func (linux *systemDRecord) Start() (string, error) {
func (linux *systemDRecord) Stop() (string, error) {
stopAction := "Stopping " + linux.description + ":"

if checkPrivileges() == false {
return stopAction + failed, errors.New(rootPrivileges)
if ok, err := checkPrivileges(); !ok {
return stopAction + failed, err
}

if linux.checkInstalled() == false {
return stopAction + failed, errors.New(linux.description + " is not installed")
if !linux.isInstalled() {
return stopAction + failed, ErrNotInstalled
}

if _, status := linux.checkRunning(); status == false {
return stopAction + failed, errors.New("service already stopped")
if _, ok := linux.checkRunning(); !ok {
return stopAction + failed, ErrAlreadyStopped
}

if err := exec.Command("systemctl", "stop", linux.name+".service").Run(); err != nil {
Expand All @@ -174,12 +173,12 @@ func (linux *systemDRecord) Stop() (string, error) {
// Status - Get service status
func (linux *systemDRecord) Status() (string, error) {

if checkPrivileges() == false {
return "", errors.New(rootPrivileges)
if ok, err := checkPrivileges(); !ok {
return "", err
}

if linux.checkInstalled() == false {
return "Status could not defined", errors.New(linux.description + " is not installed")
if !linux.isInstalled() {
return "Status could not defined", ErrNotInstalled
}

statusAction, _ := linux.checkRunning()
Expand Down
55 changes: 27 additions & 28 deletions daemon_linux_systemv.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// Copyright 2015 Igor Dolzhikov. All rights reserved.
// Copyright 2016 Igor Dolzhikov. All rights reserved.
// Use of this source code is governed by
// license that can be found in the LICENSE file.

package daemon

import (
"errors"
"os"
"os/exec"
"regexp"
Expand All @@ -24,8 +23,8 @@ func (linux *systemVRecord) servicePath() string {
return "/etc/init.d/" + linux.name
}

// Check service is installed
func (linux *systemVRecord) checkInstalled() bool {
// Is a service installed
func (linux *systemVRecord) isInstalled() bool {

if _, err := os.Stat(linux.servicePath()); err == nil {
return true
Expand Down Expand Up @@ -55,14 +54,14 @@ func (linux *systemVRecord) checkRunning() (string, bool) {
func (linux *systemVRecord) Install() (string, error) {
installAction := "Install " + linux.description + ":"

if checkPrivileges() == false {
return installAction + failed, errors.New(rootPrivileges)
if ok, err := checkPrivileges(); !ok {
return installAction + failed, err
}

srvPath := linux.servicePath()

if linux.checkInstalled() == true {
return installAction + failed, errors.New(linux.description + " already installed")
if linux.isInstalled() {
return installAction + failed, ErrAlreadyInstalled
}

file, err := os.Create(srvPath)
Expand Down Expand Up @@ -112,12 +111,12 @@ func (linux *systemVRecord) Install() (string, error) {
func (linux *systemVRecord) Remove() (string, error) {
removeAction := "Removing " + linux.description + ":"

if checkPrivileges() == false {
return removeAction + failed, errors.New(rootPrivileges)
if ok, err := checkPrivileges(); !ok {
return removeAction + failed, err
}

if linux.checkInstalled() == false {
return removeAction + failed, errors.New(linux.description + " is not installed")
if !linux.isInstalled() {
return removeAction + failed, ErrNotInstalled
}

if err := os.Remove(linux.servicePath()); err != nil {
Expand All @@ -142,16 +141,16 @@ func (linux *systemVRecord) Remove() (string, error) {
func (linux *systemVRecord) Start() (string, error) {
startAction := "Starting " + linux.description + ":"

if checkPrivileges() == false {
return startAction + failed, errors.New(rootPrivileges)
if ok, err := checkPrivileges(); !ok {
return startAction + failed, err
}

if linux.checkInstalled() == false {
return startAction + failed, errors.New(linux.description + " is not installed")
if !linux.isInstalled() {
return startAction + failed, ErrNotInstalled
}

if _, status := linux.checkRunning(); status == true {
return startAction + failed, errors.New("service already running")
if _, ok := linux.checkRunning(); ok {
return startAction + failed, ErrAlreadyRunning
}

if err := exec.Command("service", linux.name, "start").Run(); err != nil {
Expand All @@ -165,16 +164,16 @@ func (linux *systemVRecord) Start() (string, error) {
func (linux *systemVRecord) Stop() (string, error) {
stopAction := "Stopping " + linux.description + ":"

if checkPrivileges() == false {
return stopAction + failed, errors.New(rootPrivileges)
if ok, err := checkPrivileges(); !ok {
return stopAction + failed, err
}

if linux.checkInstalled() == false {
return stopAction + failed, errors.New(linux.description + " is not installed")
if !linux.isInstalled() {
return stopAction + failed, ErrNotInstalled
}

if _, status := linux.checkRunning(); status == false {
return stopAction + failed, errors.New("service already stopped")
if _, ok := linux.checkRunning(); !ok {
return stopAction + failed, ErrAlreadyStopped
}

if err := exec.Command("service", linux.name, "stop").Run(); err != nil {
Expand All @@ -187,12 +186,12 @@ func (linux *systemVRecord) Stop() (string, error) {
// Status - Get service status
func (linux *systemVRecord) Status() (string, error) {

if checkPrivileges() == false {
return "", errors.New(rootPrivileges)
if ok, err := checkPrivileges(); !ok {
return "", err
}

if linux.checkInstalled() == false {
return "Status could not defined", errors.New(linux.description + " is not installed")
if !linux.isInstalled() {
return "Status could not defined", ErrNotInstalled
}

statusAction, _ := linux.checkRunning()
Expand Down
Loading

0 comments on commit 552d5ed

Please sign in to comment.