github.com/andrewsun2898/u-root@v6.0.1-0.20200616011413-4b2895c1b815+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  	"golang.org/x/sys/unix"
    20  )
    21  
    22  func main() {
    23  	if len(os.Args) == 2 {
    24  		newHostname := os.Args[1]
    25  
    26  		err := unix.Sethostname([]byte(newHostname))
    27  		if err != nil {
    28  			log.Fatalf("could not set hostname: %v", err)
    29  		}
    30  	} else if len(os.Args) == 1 {
    31  		hostname, err := os.Hostname()
    32  		if err != nil {
    33  			log.Fatalf("could not obtain hostname: %v", err)
    34  		}
    35  
    36  		fmt.Println(hostname)
    37  	} else {
    38  		log.Fatalf("usage: hostname [HOSTNAME]")
    39  	}
    40  }