github.com/Cloud-Foundations/Dominator@v0.3.4/imageserver/httpd/listImages.go (about)

     1  package httpd
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	"github.com/Cloud-Foundations/Dominator/lib/format"
     9  	"github.com/Cloud-Foundations/Dominator/lib/html"
    10  	"github.com/Cloud-Foundations/Dominator/lib/image"
    11  	"github.com/Cloud-Foundations/Dominator/lib/verstr"
    12  )
    13  
    14  func (s state) listImagesHandler(w http.ResponseWriter, req *http.Request) {
    15  	writer := bufio.NewWriter(w)
    16  	defer writer.Flush()
    17  	imageNames := s.imageDataBase.ListImages()
    18  	verstr.Sort(imageNames)
    19  	if req.URL.RawQuery == "output=text" {
    20  		for _, name := range imageNames {
    21  			fmt.Fprintln(writer, name)
    22  		}
    23  		return
    24  	}
    25  	fmt.Fprintln(writer, "<title>imageserver images</title>")
    26  	fmt.Fprintln(writer, `<style>
    27                            table, th, td {
    28                            border-collapse: collapse;
    29                            }
    30                            </style>`)
    31  	fmt.Fprintln(writer, "<body>")
    32  	fmt.Fprintln(writer, "<h3>")
    33  	fmt.Fprintln(writer, `<table border="1" style="width:100%">`)
    34  	tw, _ := html.NewTableWriter(writer, true,
    35  		"Name", "Data Size", "Data Inodes", "Computed Inodes", "Filter Lines",
    36  		"Triggers", "Branch", "Commit")
    37  	for _, name := range imageNames {
    38  		if img := s.imageDataBase.GetImage(name); img != nil {
    39  			writeImage(tw, name, img)
    40  		}
    41  	}
    42  	tw.Close()
    43  	fmt.Fprintln(writer, "</body>")
    44  }
    45  
    46  func writeImage(tw *html.TableWriter, name string, img *image.Image) {
    47  	tw.WriteRow("", "",
    48  		fmt.Sprintf("<a href=\"showImage?%s\">%s</a>", name, name),
    49  		fmt.Sprintf("<a href=\"listImage?%s\">%s</a>",
    50  			name, format.FormatBytes(img.FileSystem.TotalDataBytes)),
    51  		fmt.Sprintf("<a href=\"listImage?%s\">%d</a>",
    52  			name, img.FileSystem.NumRegularInodes),
    53  		func() string {
    54  			if num := img.FileSystem.NumComputedRegularInodes(); num < 1 {
    55  				return "0"
    56  			} else {
    57  				return fmt.Sprintf("<a href=\"listComputedInodes?%s\">%d</a>",
    58  					name, num)
    59  			}
    60  		}(),
    61  		func() string {
    62  			if img.Filter == nil {
    63  				return "(sparse filter)"
    64  			} else if len(img.Filter.FilterLines) < 1 {
    65  				return "0"
    66  			} else {
    67  				return fmt.Sprintf("<a href=\"listFilter?%s\">%d</a>",
    68  					name, len(img.Filter.FilterLines))
    69  			}
    70  		}(),
    71  		func() string {
    72  			if img.Triggers == nil || len(img.Triggers.Triggers) < 1 {
    73  				return "0"
    74  			} else {
    75  				return fmt.Sprintf("<a href=\"listTriggers?%s\">%d</a>",
    76  					name, len(img.Triggers.Triggers))
    77  			}
    78  		}(),
    79  		img.BuildBranch,
    80  		img.BuildCommitId,
    81  	)
    82  }