github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/interfaces/systemd/service.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016-2017 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package systemd 21 22 import ( 23 "bytes" 24 "fmt" 25 ) 26 27 // Service describes a single systemd service file 28 type Service struct { 29 Description string 30 Type string 31 RemainAfterExit bool 32 ExecStart string 33 ExecStop string 34 } 35 36 func (s *Service) String() string { 37 var buf bytes.Buffer 38 if s.Description != "" { 39 buf.WriteString("[Unit]\n") 40 fmt.Fprintf(&buf, "Description=%s\n\n", s.Description) 41 } 42 buf.WriteString("[Service]\n") 43 if s.Type != "" { 44 fmt.Fprintf(&buf, "Type=%s\n", s.Type) 45 } 46 // "no" is the default in systemd so we don't neet to write it 47 if s.RemainAfterExit { 48 buf.WriteString("RemainAfterExit=yes\n") 49 } 50 if s.ExecStart != "" { 51 fmt.Fprintf(&buf, "ExecStart=%s\n", s.ExecStart) 52 } 53 if s.ExecStop != "" { 54 fmt.Fprintf(&buf, "ExecStop=%s\n", s.ExecStop) 55 } 56 fmt.Fprintf(&buf, "\n[Install]\nWantedBy=multi-user.target\n") 57 return buf.String() 58 }