gitlab.com/apertussolutions/u-root@v7.0.0+incompatible/cmds/core/hostname/hostname.go (about)

     1  // Copyright 2012-2017 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // hostname prints or changes the system's hostname.
     6  //
     7  // Synopsis:
     8  //     hostname [HOSTNAME]
     9  //
    10  // Author:
    11  //     Beletti <rhiguita@gmail.com>
    12  package main
    13  
    14  import (
    15  	"fmt"
    16  	"log"
    17  	"os"
    18  )
    19  
    20  func main() {
    21  	a := os.Args
    22  	switch len(a) {
    23  	case 2:
    24  		if err := Sethostname(a[1]); err != nil {
    25  			log.Fatalf("could not set hostname: %v", err)
    26  		}
    27  	case 1:
    28  		hostname, err := os.Hostname()
    29  		if err != nil {
    30  			log.Fatalf("could not obtain hostname: %v", err)
    31  		}
    32  		fmt.Println(hostname)
    33  	default:
    34  		log.Fatalf("usage: hostname [HOSTNAME]")
    35  	}
    36  }