-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from ncode/juliano/systemd_and_cleanup
Draft: adds systemd and cleanup commands
- Loading branch information
Showing
5 changed files
with
243 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
Copyright © 2024 Juliano Martinez <[email protected]> | ||
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 cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/ncode/tagit/pkg/systemd" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// systemdCmd represents the systemd command | ||
var systemdCmd = &cobra.Command{ | ||
Use: "systemd", | ||
Short: "A brief description of your command", | ||
Long: `A longer description that spans multiple lines and likely contains examples | ||
and usage of using your command. For example: | ||
Cobra is a CLI library for Go that empowers applications. | ||
This application is a tool to generate the needed files | ||
to quickly create a Cobra application.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fields := &systemd.Fields{ | ||
User: cmd.PersistentFlags().Lookup("user").Value.String(), | ||
Group: cmd.PersistentFlags().Lookup("group").Value.String(), | ||
ConsulAddr: cmd.InheritedFlags().Lookup("consul-addr").Value.String(), | ||
ServiceID: cmd.InheritedFlags().Lookup("service-id").Value.String(), | ||
Script: cmd.InheritedFlags().Lookup("script").Value.String(), | ||
TagPrefix: cmd.InheritedFlags().Lookup("tag-prefix").Value.String(), | ||
Interval: cmd.InheritedFlags().Lookup("interval").Value.String(), | ||
Token: cmd.InheritedFlags().Lookup("token").Value.String(), | ||
} | ||
fmt.Println(systemd.RenderTemplate(fields)) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(systemdCmd) | ||
systemdCmd.PersistentFlags().StringP("user", "u", "nobody", "user to run the service") | ||
systemdCmd.PersistentFlags().StringP("group", "g", "nobody", "group to run the service") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package systemd | ||
|
||
import ( | ||
"bytes" | ||
"text/template" | ||
) | ||
|
||
// Fields is the struct that holds the fields for the systemd service. | ||
type Fields struct { | ||
ServiceID string | ||
Script string | ||
TagPrefix string | ||
Interval string | ||
Token string | ||
ConsulAddr string | ||
User string | ||
Group string | ||
} | ||
|
||
// serviceTemplate is the template for the systemd service. | ||
var serviceTemplate = ` | ||
[Unit] | ||
Description=Tagit {{ .ServiceID }} | ||
After=network.target | ||
After=network-online.target | ||
Wants=network-online.target | ||
[Service] | ||
Type=simple | ||
ExecStart=/usr/bin/tagit run -s {{ .ServiceID }} -x {{ .Script }} -p {{ .TagPrefix }} -i {{ .Interval }}{{- if .Token }} -t {{ .Token }}{{- end }}{{ if .ConsulAddr }} -c {{ .ConsulAddr }}{{- end }} | ||
Environment=HOME=/var/run/taggit/{{ .ServiceID }} | ||
Restart=always | ||
User={{ .User }} | ||
Group={{ .Group }} | ||
[Install] | ||
WantedBy=multi-user.target | ||
` | ||
|
||
// RenderTemplate renders the template for the systemd service. | ||
func RenderTemplate(fields *Fields) (string, error) { | ||
tmpl, err := template.New("serviceTemplate").Parse(serviceTemplate) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var tmplBuffer bytes.Buffer | ||
err = tmpl.Execute(&tmplBuffer, fields) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return tmplBuffer.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package systemd | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestRenderTemplate(t *testing.T) { | ||
// Define a test case struct | ||
type testCase struct { | ||
name string | ||
fields Fields | ||
wantErr bool | ||
checkStr string // A substring we expect in the output | ||
expectStr bool // A flag to indicate the absence of a substring | ||
} | ||
|
||
// Define your test cases | ||
testCases := []testCase{ | ||
{ | ||
name: "Basic test", | ||
fields: Fields{ | ||
ServiceID: "testservice", | ||
Script: "testscript", | ||
TagPrefix: "testprefix", | ||
Interval: "testinterval", | ||
Token: "testtoken", | ||
ConsulAddr: "testaddr", | ||
User: "testuser", | ||
Group: "testgroup", | ||
}, | ||
wantErr: false, | ||
checkStr: "ExecStart=/usr/bin/tagit run -s testservice -x testscript", | ||
expectStr: true, | ||
}, | ||
{ | ||
name: "With Token", | ||
fields: Fields{ | ||
ServiceID: "testservice", | ||
Token: "sometoken", | ||
}, | ||
wantErr: false, | ||
checkStr: "-t sometoken", | ||
expectStr: true, | ||
}, | ||
{ | ||
name: "Without Token", | ||
fields: Fields{ | ||
ServiceID: "testservice", | ||
}, | ||
wantErr: false, | ||
checkStr: "-t", | ||
expectStr: false, | ||
}, | ||
{ | ||
name: "With Consul Address", | ||
fields: Fields{ | ||
ServiceID: "testservice", | ||
ConsulAddr: "someaddress", | ||
}, | ||
wantErr: false, | ||
checkStr: "-c someaddress", | ||
expectStr: true, | ||
}, | ||
{ | ||
name: "Without Consul Address", | ||
fields: Fields{ | ||
ServiceID: "testservice", | ||
}, | ||
wantErr: false, | ||
checkStr: "-c someaddress", | ||
expectStr: false, | ||
}, | ||
{ | ||
name: "Empty Fields", | ||
fields: Fields{}, | ||
wantErr: false, | ||
checkStr: "ExecStart=/usr/bin/tagit run -s", | ||
expectStr: true, | ||
}, | ||
{ | ||
name: "Consul Address Only", | ||
fields: Fields{ | ||
ConsulAddr: "127.0.0.1", | ||
}, | ||
wantErr: false, | ||
checkStr: "-c 127.0.0.1", | ||
expectStr: true, | ||
}, | ||
} | ||
|
||
// Iterate over the test cases | ||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
got, err := RenderTemplate(&tc.fields) | ||
if (err != nil) != tc.wantErr { | ||
t.Errorf("RenderTemplate() error = %v, wantErr %v", err, tc.wantErr) | ||
return | ||
} | ||
|
||
// Check for the presence or absence of the token | ||
if tc.expectStr && !strings.Contains(got, tc.checkStr) { | ||
t.Errorf("RenderTemplate() = %v, want %v", got, tc.checkStr) | ||
} else if !tc.expectStr && strings.Contains(got, tc.checkStr) { | ||
t.Errorf("RenderTemplate() = %v, should not contain %v", got, tc.checkStr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestRenderTemplateFailure(t *testing.T) { | ||
_, err := RenderTemplate(nil) | ||
if err == nil { | ||
t.Errorf("RenderTemplate() with nil input did not fail as expected") | ||
} | ||
} |