Skip to content

Commit

Permalink
Move invocation from main to run (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
patricksanders authored Dec 11, 2020
1 parent 63b0e6c commit 27da05a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 45 deletions.
47 changes: 2 additions & 45 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,59 +18,16 @@ package main

import (
"os"
"os/signal"
"syscall"

"github.com/kardianos/service"
"github.com/netflix/weep/cmd"
"github.com/netflix/weep/run"
log "github.com/sirupsen/logrus"
)

var svcLogger service.Logger
var done chan int

func init() {
// Output to stdout instead of the default stderr
log.SetOutput(os.Stdout)
}

type program struct{}

func (p *program) Start(s service.Service) error {
go p.run()
return nil
}

func (p *program) run() {
shutdown := make(chan os.Signal, 1)
done = make(chan int, 1)
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)
cmd.Execute(shutdown, done)
}

func (p *program) Stop(s service.Service) error {
<-done
return nil
}

func main() {
svcConfig := &service.Config{
Name: "Weep",
DisplayName: "Weep",
Description: "The ConsoleMe CLI",
}

prg := &program{}
s, err := service.New(prg, svcConfig)
if err != nil {
log.Fatal(err)
}
svcLogger, err = s.Logger(nil)
if err != nil {
log.Fatal(err)
}
err = s.Run()
if err != nil {
_ = svcLogger.Error(err)
}
run.Run()
}
71 changes: 71 additions & 0 deletions run/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2020 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package run

import (
"os"
"os/signal"
"syscall"

"github.com/kardianos/service"
"github.com/netflix/weep/cmd"
log "github.com/sirupsen/logrus"
)

var svcLogger service.Logger
var done chan int

type program struct{}

func (p *program) Start(s service.Service) error {
go p.run()
return nil
}

func (p *program) run() {
shutdown := make(chan os.Signal, 1)
done = make(chan int, 1)
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)
cmd.Execute(shutdown, done)
}

func (p *program) Stop(s service.Service) error {
<-done
return nil
}

func Run() {
svcConfig := &service.Config{
Name: "Weep",
DisplayName: "Weep",
Description: "The ConsoleMe CLI",
}

prg := &program{}
s, err := service.New(prg, svcConfig)
if err != nil {
log.Fatal(err)
}
svcLogger, err = s.Logger(nil)
if err != nil {
log.Fatal(err)
}
err = s.Run()
if err != nil {
_ = svcLogger.Error(err)
}
}

0 comments on commit 27da05a

Please sign in to comment.