Coder Social home page Coder Social logo

go-systemdconf's Introduction

systemdconf

Travis-CI AppVeyor GoDoc Report card

Package systemdconf implements encoding and decoding of systemd configuration files as defined by systemd.syntax (see https://www.freedesktop.org/software/systemd/man/systemd.syntax.html for details).

The mapping between systemd and Go is described in the documentation for the Marshal and Unmarshal functions

Installation

Use go get:

go get github.com/sergeymakinen/go-systemdconf

Then import the package into your own code:

import "github.com/sergeymakinen/go-systemdconf"

Example

Marshal

service := unit.ServiceFile{
    Unit: unit.UnitSection{
        Description: systemdconf.Value{"Simple firewall"},
    },
    Service: unit.ServiceSection{
        Type:            systemdconf.Value{"oneshot"},
        RemainAfterExit: systemdconf.Value{"yes"},
        ExecStart: systemdconf.Value{
            "/usr/local/sbin/simple-firewall-start1",
            "/usr/local/sbin/simple-firewall-start2",
        },
        ExecStop: systemdconf.Value{"/usr/local/sbin/simple-firewall-stop"},
    },
    Install: unit.InstallSection{
        WantedBy: systemdconf.Value{"multi-user.target"},
    },
}
b, _ := systemdconf.Marshal(service)

fmt.Println(string(b))
// Output: [Unit]
// Description=Simple firewall
//
// [Service]
// Type=oneshot
// RemainAfterExit=yes
// ExecStart=/usr/local/sbin/simple-firewall-start1
// ExecStart=/usr/local/sbin/simple-firewall-start2
// ExecStop=/usr/local/sbin/simple-firewall-stop
//
// [Install]
// WantedBy=multi-user.target

Unmarshal

file := `[Unit]
Description=Simple firewall

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/sbin/simple-firewall-start
ExecStop=/usr/local/sbin/simple-firewall-stop

[Install]
WantedBy=multi-user.target`

var service unit.ServiceFile
systemdconf.Unmarshal([]byte(file), &service)

fmt.Println(service.Unit.Description)
b, _ := service.Service.RemainAfterExit.Bool()
if b {
    fmt.Println(b)
}
// Output: Simple firewall
// true

License

BSD 3-Clause

go-systemdconf's People

Contributors

sergeymakinen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.