项目作者: raphink

项目描述 :
Map configuration files to Go structures using Augeas
高级语言: Go
项目地址: git://github.com/raphink/narcissus.git
创建时间: 2016-08-11T07:24:58Z
项目社区:https://github.com/raphink/narcissus

开源协议:Apache License 2.0

下载


Narcissus: edit configuration files as go structs

Documentation
Build Status
Coverage Status
Go Report Card

This go package aims to provide reflection for the Augeas library.

This allows to turn Augeas lenses into Go structs for easy parsing and
modification of configuration files.

Example

  1. import (
  2. "log"
  3. "honnef.co/go/augeas"
  4. "github.com/raphink/narcissus"
  5. )
  6. func main() {
  7. aug, err := augeas.New("/", "", augeas.None)
  8. if err != nil {
  9. log.Fatal("Failed to create Augeas handler")
  10. }
  11. n := narcissus.New(&aug)
  12. user := n.NewPasswdUser("raphink")
  13. if err != nil {
  14. log.Fatalf("Failed to retrieve user: %v" err)
  15. }
  16. log.Printf("UID=%v", user.UID)
  17. // Modify UID
  18. user.UID = 42
  19. err = n.Write(user)
  20. if err != nil {
  21. log.Fatalf("Failed to save user: %v", err)
  22. }
  23. }

Available types

Fstab

  • Fstab maps a whole /etc/fstab file
  • FstabEntry maps a single /etc/fstab entry

Hosts

  • Hosts maps a whole /etc/hosts file
  • Host maps a single /etc/hosts entry

Passwd

Services

  • Services maps a whole /etc/services file
  • Service maps a single /etc/services entry

Mapping your own structures

  1. import (
  2. "log"
  3. "honnef.co/go/augeas"
  4. "github.com/raphink/narcissus"
  5. )
  6. type group struct {
  7. augeasPath string
  8. Name string `narcissus:".,value-from-label"`
  9. Password string `narcissus:"password"`
  10. GID int `narcissus:"gid"`
  11. Users []string `narcissus:"user"`
  12. }
  13. func main() {
  14. aug, err := augeas.New("/", "", augeas.None)
  15. if err != nil {
  16. log.Fatal("Failed to create Augeas handler")
  17. }
  18. n := narcissus.New(&aug)
  19. group := &group{
  20. augeasPath: "/files/etc/group/docker",
  21. }
  22. err = n.Parse(group)
  23. if err != nil {
  24. log.Fatalf("Failed to retrieve group: %v", err)
  25. }
  26. log.Printf("GID=%v", group.GID)
  27. log.Printf("Users=%v", strings.Join(group.Users, ","))
  28. }

Tag values

The narcissus tag accepts multiple comma separated values. The first value in
the list is the relative path where the field is mapped in the Augeas tree.

Other possible (optional) values are:

  • value-from-label: get field value from the node label instead of its value;
  • seq (slice field only): will treat field as a seq entry in the Augeas tree;
  • key-from-value (map field only): get the key from the node label instead
    of its value;
  • purge (map field only): purge all unknown keys in the map.

Fields

Described structures have special fields to specify parameters for Augeas.

  • augeasPath (mandatory): the path to the structure in the Augeas tree;
  • augeasFile (optional): let Augeas load only this file. If augeasLens is
    not specified, Augeas will use the default lens for the file if available;
  • augeasLens (optional): if augeasFile is set (ignored otherwise),
    specifies which lens to use to parse the file. This is required when parsing
    a file at a non-standard location.

Each of these fields can be specified in one of two ways:

  • by using the default tag with a default value for the field, e.g.
  1. type group struct {
  2. augeasPath string `default:"/files/etc/group/root"`
  3. }
  • by specifying a value for the instance in the structure field, e.g.
  1. myGroup := group {
  2. augeasPath: "/files/etc/group/docker",
  3. }