cmd/cue/cmd: get go: convert Go block comments

Also fix Kubernetes tutorial test which
failed to remove stale files.

Change-Id: Id8718fe38b71a4995a25d0e154b84890ac72f70e
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/3872
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cmd/cue/cmd/get_go.go b/cmd/cue/cmd/get_go.go
index 03bb4df..4229fd7 100644
--- a/cmd/cue/cmd/get_go.go
+++ b/cmd/cue/cmd/get_go.go
@@ -725,6 +725,65 @@
 	return ""
 }
 
+func writeDoc(w io.Writer, g *ast.CommentGroup) {
+	if g == nil {
+		return
+	}
+
+	for _, comment := range g.List {
+		c := comment.Text
+
+		// Remove comment markers.
+		// The parser has given us exactly the comment text.
+		switch c[1] {
+		case '/':
+			//-style comment (no newline at the end)
+			_, _ = fmt.Fprintf(w, "//%s\n", c[2:])
+
+		case '*':
+			/*-style comment */
+			c = c[2 : len(c)-2]
+			if len(c) > 0 && c[0] == '\n' {
+				c = c[1:]
+			}
+
+			lines := strings.Split(c, "\n")
+
+			// Find common space prefix
+			i := 0
+			line := lines[0]
+			for ; i < len(line); i++ {
+				if c := line[i]; c != ' ' && c != '\t' {
+					break
+				}
+			}
+
+			for _, l := range lines {
+				for j := 0; j < i && j < len(l); j++ {
+					if line[j] != l[j] {
+						i = j
+						break
+					}
+				}
+			}
+
+			// Strip last line if empty.
+			if n := len(lines); n > 1 && len(lines[n-1]) < i {
+				lines = lines[:n-1]
+			}
+
+			// Print lines.
+			for _, l := range lines {
+				if i >= len(l) {
+					_, _ = io.WriteString(w, "//\n")
+					continue
+				}
+				_, _ = fmt.Fprintf(w, "// %s\n", l[i:])
+			}
+		}
+	}
+}
+
 func (e *extractor) printDoc(doc *ast.CommentGroup, newline bool) {
 	if doc == nil {
 		return
@@ -732,10 +791,7 @@
 	if newline {
 		e.newLine()
 	}
-	for _, c := range doc.List {
-		fmt.Fprint(e.w, c.Text)
-		e.newLine()
-	}
+	writeDoc(e.w, doc)
 }
 
 func (e *extractor) newLine() {
diff --git a/cmd/cue/cmd/get_go_test.go b/cmd/cue/cmd/get_go_test.go
index 798e3a5..bd69476 100644
--- a/cmd/cue/cmd/get_go_test.go
+++ b/cmd/cue/cmd/get_go_test.go
@@ -95,7 +95,7 @@
 		t.Fatalf("could not load file %s", path)
 	}
 	// Strip comments up till package clause. Local packages will generate
-	// differently using GOPATH versuse modules.
+	// differently using GOPATH versus modules.
 	s := string(b)
 	return s[strings.Index(s, "package"):]
 }
diff --git a/cmd/cue/cmd/testdata/code/go/pkg1/file1.go b/cmd/cue/cmd/testdata/code/go/pkg1/file1.go
index eaa4f2e..99fd121 100644
--- a/cmd/cue/cmd/testdata/code/go/pkg1/file1.go
+++ b/cmd/cue/cmd/testdata/code/go/pkg1/file1.go
@@ -68,6 +68,12 @@
 type Level int
 
 const (
+	/*
+		Block comment.
+			Indented.
+
+		Empty line before.
+	*/
 	Unknown Level = iota
 	Low
 	// Medium is neither High nor Low
diff --git a/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg1/file1_go_gen.cue b/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg1/file1_go_gen.cue
index 653f748..995d56a 100644
--- a/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg1/file1_go_gen.cue
+++ b/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg1/file1_go_gen.cue
@@ -55,6 +55,10 @@
 	Medium |
 	High
 
+// Block comment.
+//  Indented.
+//
+// Empty line before.
 Unknown :: Level & 0
 Low ::     Level & 1
 
diff --git a/doc/tutorial/kubernetes/quick/cue.mod/gen/k8s.io/api/apps/v1beta1/generated.pb_go_gen.cue b/doc/tutorial/kubernetes/quick/cue.mod/gen/k8s.io/api/apps/v1beta1/generated.pb_go_gen.cue
deleted file mode 100644
index 43b9795..0000000
--- a/doc/tutorial/kubernetes/quick/cue.mod/gen/k8s.io/api/apps/v1beta1/generated.pb_go_gen.cue
+++ /dev/null
@@ -1,34 +0,0 @@
-// Code generated by cue get go. DO NOT EDIT.
-
-//cue:generate cue get go k8s.io/api/apps/v1beta1
-
-/*
-	Package v1beta1 is a generated protocol buffer package.
-
-	It is generated from these files:
-		k8s.io/kubernetes/vendor/k8s.io/api/apps/v1beta1/generated.proto
-
-	It has these top-level messages:
-		ControllerRevision
-		ControllerRevisionList
-		Deployment
-		DeploymentCondition
-		DeploymentList
-		DeploymentRollback
-		DeploymentSpec
-		DeploymentStatus
-		DeploymentStrategy
-		RollbackConfig
-		RollingUpdateDeployment
-		RollingUpdateStatefulSetStrategy
-		Scale
-		ScaleSpec
-		ScaleStatus
-		StatefulSet
-		StatefulSetCondition
-		StatefulSetList
-		StatefulSetSpec
-		StatefulSetStatus
-		StatefulSetUpdateStrategy
-*/
-package v1beta1
diff --git a/doc/tutorial/kubernetes/quick/cue.mod/gen/k8s.io/api/core/v1/generated.pb_go_gen.cue b/doc/tutorial/kubernetes/quick/cue.mod/gen/k8s.io/api/core/v1/generated.pb_go_gen.cue
deleted file mode 100644
index 7fb1095..0000000
--- a/doc/tutorial/kubernetes/quick/cue.mod/gen/k8s.io/api/core/v1/generated.pb_go_gen.cue
+++ /dev/null
@@ -1,211 +0,0 @@
-// Code generated by cue get go. DO NOT EDIT.
-
-//cue:generate cue get go k8s.io/api/core/v1
-
-/*
-	Package v1 is a generated protocol buffer package.
-
-	It is generated from these files:
-		k8s.io/kubernetes/vendor/k8s.io/api/core/v1/generated.proto
-
-	It has these top-level messages:
-		AWSElasticBlockStoreVolumeSource
-		Affinity
-		AttachedVolume
-		AvoidPods
-		AzureDiskVolumeSource
-		AzureFilePersistentVolumeSource
-		AzureFileVolumeSource
-		Binding
-		CSIPersistentVolumeSource
-		CSIVolumeSource
-		Capabilities
-		CephFSPersistentVolumeSource
-		CephFSVolumeSource
-		CinderPersistentVolumeSource
-		CinderVolumeSource
-		ClientIPConfig
-		ComponentCondition
-		ComponentStatus
-		ComponentStatusList
-		ConfigMap
-		ConfigMapEnvSource
-		ConfigMapKeySelector
-		ConfigMapList
-		ConfigMapNodeConfigSource
-		ConfigMapProjection
-		ConfigMapVolumeSource
-		Container
-		ContainerImage
-		ContainerPort
-		ContainerState
-		ContainerStateRunning
-		ContainerStateTerminated
-		ContainerStateWaiting
-		ContainerStatus
-		DaemonEndpoint
-		DownwardAPIProjection
-		DownwardAPIVolumeFile
-		DownwardAPIVolumeSource
-		EmptyDirVolumeSource
-		EndpointAddress
-		EndpointPort
-		EndpointSubset
-		Endpoints
-		EndpointsList
-		EnvFromSource
-		EnvVar
-		EnvVarSource
-		Event
-		EventList
-		EventSeries
-		EventSource
-		ExecAction
-		FCVolumeSource
-		FlexPersistentVolumeSource
-		FlexVolumeSource
-		FlockerVolumeSource
-		GCEPersistentDiskVolumeSource
-		GitRepoVolumeSource
-		GlusterfsPersistentVolumeSource
-		GlusterfsVolumeSource
-		HTTPGetAction
-		HTTPHeader
-		Handler
-		HostAlias
-		HostPathVolumeSource
-		ISCSIPersistentVolumeSource
-		ISCSIVolumeSource
-		KeyToPath
-		Lifecycle
-		LimitRange
-		LimitRangeItem
-		LimitRangeList
-		LimitRangeSpec
-		List
-		LoadBalancerIngress
-		LoadBalancerStatus
-		LocalObjectReference
-		LocalVolumeSource
-		NFSVolumeSource
-		Namespace
-		NamespaceList
-		NamespaceSpec
-		NamespaceStatus
-		Node
-		NodeAddress
-		NodeAffinity
-		NodeCondition
-		NodeConfigSource
-		NodeConfigStatus
-		NodeDaemonEndpoints
-		NodeList
-		NodeProxyOptions
-		NodeResources
-		NodeSelector
-		NodeSelectorRequirement
-		NodeSelectorTerm
-		NodeSpec
-		NodeStatus
-		NodeSystemInfo
-		ObjectFieldSelector
-		ObjectReference
-		PersistentVolume
-		PersistentVolumeClaim
-		PersistentVolumeClaimCondition
-		PersistentVolumeClaimList
-		PersistentVolumeClaimSpec
-		PersistentVolumeClaimStatus
-		PersistentVolumeClaimVolumeSource
-		PersistentVolumeList
-		PersistentVolumeSource
-		PersistentVolumeSpec
-		PersistentVolumeStatus
-		PhotonPersistentDiskVolumeSource
-		Pod
-		PodAffinity
-		PodAffinityTerm
-		PodAntiAffinity
-		PodAttachOptions
-		PodCondition
-		PodDNSConfig
-		PodDNSConfigOption
-		PodExecOptions
-		PodList
-		PodLogOptions
-		PodPortForwardOptions
-		PodProxyOptions
-		PodReadinessGate
-		PodSecurityContext
-		PodSignature
-		PodSpec
-		PodStatus
-		PodStatusResult
-		PodTemplate
-		PodTemplateList
-		PodTemplateSpec
-		PortworxVolumeSource
-		Preconditions
-		PreferAvoidPodsEntry
-		PreferredSchedulingTerm
-		Probe
-		ProjectedVolumeSource
-		QuobyteVolumeSource
-		RBDPersistentVolumeSource
-		RBDVolumeSource
-		RangeAllocation
-		ReplicationController
-		ReplicationControllerCondition
-		ReplicationControllerList
-		ReplicationControllerSpec
-		ReplicationControllerStatus
-		ResourceFieldSelector
-		ResourceQuota
-		ResourceQuotaList
-		ResourceQuotaSpec
-		ResourceQuotaStatus
-		ResourceRequirements
-		SELinuxOptions
-		ScaleIOPersistentVolumeSource
-		ScaleIOVolumeSource
-		ScopeSelector
-		ScopedResourceSelectorRequirement
-		Secret
-		SecretEnvSource
-		SecretKeySelector
-		SecretList
-		SecretProjection
-		SecretReference
-		SecretVolumeSource
-		SecurityContext
-		SerializedReference
-		Service
-		ServiceAccount
-		ServiceAccountList
-		ServiceAccountTokenProjection
-		ServiceList
-		ServicePort
-		ServiceProxyOptions
-		ServiceSpec
-		ServiceStatus
-		SessionAffinityConfig
-		StorageOSPersistentVolumeSource
-		StorageOSVolumeSource
-		Sysctl
-		TCPSocketAction
-		Taint
-		Toleration
-		TopologySelectorLabelRequirement
-		TopologySelectorTerm
-		TypedLocalObjectReference
-		Volume
-		VolumeDevice
-		VolumeMount
-		VolumeNodeAffinity
-		VolumeProjection
-		VolumeSource
-		VsphereVirtualDiskVolumeSource
-		WeightedPodAffinityTerm
-		WindowsSecurityContextOptions
-*/
-package v1
diff --git a/doc/tutorial/kubernetes/quick/cue.mod/gen/k8s.io/api/extensions/v1beta1/generated.pb_go_gen.cue b/doc/tutorial/kubernetes/quick/cue.mod/gen/k8s.io/api/extensions/v1beta1/generated.pb_go_gen.cue
deleted file mode 100644
index 24b6fcb..0000000
--- a/doc/tutorial/kubernetes/quick/cue.mod/gen/k8s.io/api/extensions/v1beta1/generated.pb_go_gen.cue
+++ /dev/null
@@ -1,70 +0,0 @@
-// Code generated by cue get go. DO NOT EDIT.
-
-//cue:generate cue get go k8s.io/api/extensions/v1beta1
-
-/*
-	Package v1beta1 is a generated protocol buffer package.
-
-	It is generated from these files:
-		k8s.io/kubernetes/vendor/k8s.io/api/extensions/v1beta1/generated.proto
-
-	It has these top-level messages:
-		AllowedCSIDriver
-		AllowedFlexVolume
-		AllowedHostPath
-		DaemonSet
-		DaemonSetCondition
-		DaemonSetList
-		DaemonSetSpec
-		DaemonSetStatus
-		DaemonSetUpdateStrategy
-		Deployment
-		DeploymentCondition
-		DeploymentList
-		DeploymentRollback
-		DeploymentSpec
-		DeploymentStatus
-		DeploymentStrategy
-		FSGroupStrategyOptions
-		HTTPIngressPath
-		HTTPIngressRuleValue
-		HostPortRange
-		IDRange
-		IPBlock
-		Ingress
-		IngressBackend
-		IngressList
-		IngressRule
-		IngressRuleValue
-		IngressSpec
-		IngressStatus
-		IngressTLS
-		NetworkPolicy
-		NetworkPolicyEgressRule
-		NetworkPolicyIngressRule
-		NetworkPolicyList
-		NetworkPolicyPeer
-		NetworkPolicyPort
-		NetworkPolicySpec
-		PodSecurityPolicy
-		PodSecurityPolicyList
-		PodSecurityPolicySpec
-		ReplicaSet
-		ReplicaSetCondition
-		ReplicaSetList
-		ReplicaSetSpec
-		ReplicaSetStatus
-		ReplicationControllerDummy
-		RollbackConfig
-		RollingUpdateDaemonSet
-		RollingUpdateDeployment
-		RunAsGroupStrategyOptions
-		RunAsUserStrategyOptions
-		RuntimeClassStrategyOptions
-		SELinuxStrategyOptions
-		Scale
-		ScaleSpec
-		ScaleStatus
-		SupplementalGroupsStrategyOptions
-*/
-package v1beta1
diff --git a/doc/tutorial/kubernetes/quick/cue.mod/gen/k8s.io/apimachinery/pkg/api/resource/generated.pb_go_gen.cue b/doc/tutorial/kubernetes/quick/cue.mod/gen/k8s.io/apimachinery/pkg/api/resource/generated.pb_go_gen.cue
deleted file mode 100644
index a45b201..0000000
--- a/doc/tutorial/kubernetes/quick/cue.mod/gen/k8s.io/apimachinery/pkg/api/resource/generated.pb_go_gen.cue
+++ /dev/null
@@ -1,14 +0,0 @@
-// Code generated by cue get go. DO NOT EDIT.
-
-//cue:generate cue get go k8s.io/apimachinery/pkg/api/resource
-
-/*
-Package resource is a generated protocol buffer package.
-
-It is generated from these files:
-	k8s.io/kubernetes/vendor/k8s.io/apimachinery/pkg/api/resource/generated.proto
-
-It has these top-level messages:
-	Quantity
-*/
-package resource
diff --git a/doc/tutorial/kubernetes/quick/cue.mod/gen/k8s.io/apimachinery/pkg/apis/meta/v1/generated.pb_go_gen.cue b/doc/tutorial/kubernetes/quick/cue.mod/gen/k8s.io/apimachinery/pkg/apis/meta/v1/generated.pb_go_gen.cue
deleted file mode 100644
index 7c54b85..0000000
--- a/doc/tutorial/kubernetes/quick/cue.mod/gen/k8s.io/apimachinery/pkg/apis/meta/v1/generated.pb_go_gen.cue
+++ /dev/null
@@ -1,58 +0,0 @@
-// Code generated by cue get go. DO NOT EDIT.
-
-//cue:generate cue get go k8s.io/apimachinery/pkg/apis/meta/v1
-
-/*
-	Package v1 is a generated protocol buffer package.
-
-	It is generated from these files:
-		k8s.io/kubernetes/vendor/k8s.io/apimachinery/pkg/apis/meta/v1/generated.proto
-
-	It has these top-level messages:
-		APIGroup
-		APIGroupList
-		APIResource
-		APIResourceList
-		APIVersions
-		CreateOptions
-		DeleteOptions
-		Duration
-		ExportOptions
-		Fields
-		GetOptions
-		GroupKind
-		GroupResource
-		GroupVersion
-		GroupVersionForDiscovery
-		GroupVersionKind
-		GroupVersionResource
-		Initializer
-		Initializers
-		LabelSelector
-		LabelSelectorRequirement
-		List
-		ListMeta
-		ListOptions
-		ManagedFieldsEntry
-		MicroTime
-		ObjectMeta
-		OwnerReference
-		PartialObjectMetadata
-		PartialObjectMetadataList
-		Patch
-		PatchOptions
-		Preconditions
-		RootPaths
-		ServerAddressByClientCIDR
-		Status
-		StatusCause
-		StatusDetails
-		TableOptions
-		Time
-		Timestamp
-		TypeMeta
-		UpdateOptions
-		Verbs
-		WatchEvent
-*/
-package v1
diff --git a/doc/tutorial/kubernetes/quick/cue.mod/gen/k8s.io/apimachinery/pkg/apis/meta/v1/types_go_gen.cue b/doc/tutorial/kubernetes/quick/cue.mod/gen/k8s.io/apimachinery/pkg/apis/meta/v1/types_go_gen.cue
index 9632b2b..0ab7472 100644
--- a/doc/tutorial/kubernetes/quick/cue.mod/gen/k8s.io/apimachinery/pkg/apis/meta/v1/types_go_gen.cue
+++ b/doc/tutorial/kubernetes/quick/cue.mod/gen/k8s.io/apimachinery/pkg/apis/meta/v1/types_go_gen.cue
@@ -338,9 +338,6 @@
 	// If this is not a watch, this field is ignored.
 	// If the feature gate WatchBookmarks is not enabled in apiserver,
 	// this field is ignored.
-	//
-	// This field is beta.
-	//
 	// +optional
 	allowWatchBookmarks?: bool @go(AllowWatchBookmarks) @protobuf(9,varint,opt)
 
@@ -1289,6 +1286,7 @@
 IncludeObject :: IncludeObjectPolicy & "Object"
 
 // TableOptions are used when a Table is requested by the caller.
+// +k8s:conversion-gen:explicit-from=net/url.Values
 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
 TableOptions :: {
 	TypeMeta
diff --git a/doc/tutorial/kubernetes/quick/cue.mod/gen/k8s.io/apimachinery/pkg/runtime/generated.pb_go_gen.cue b/doc/tutorial/kubernetes/quick/cue.mod/gen/k8s.io/apimachinery/pkg/runtime/generated.pb_go_gen.cue
deleted file mode 100644
index 53b5f49..0000000
--- a/doc/tutorial/kubernetes/quick/cue.mod/gen/k8s.io/apimachinery/pkg/runtime/generated.pb_go_gen.cue
+++ /dev/null
@@ -1,16 +0,0 @@
-// Code generated by cue get go. DO NOT EDIT.
-
-//cue:generate cue get go k8s.io/apimachinery/pkg/runtime
-
-/*
-	Package runtime is a generated protocol buffer package.
-
-	It is generated from these files:
-		k8s.io/kubernetes/vendor/k8s.io/apimachinery/pkg/runtime/generated.proto
-
-	It has these top-level messages:
-		RawExtension
-		TypeMeta
-		Unknown
-*/
-package runtime
diff --git a/doc/tutorial/kubernetes/quick/cue.mod/gen/k8s.io/apimachinery/pkg/util/intstr/generated.pb_go_gen.cue b/doc/tutorial/kubernetes/quick/cue.mod/gen/k8s.io/apimachinery/pkg/util/intstr/generated.pb_go_gen.cue
deleted file mode 100644
index c97c11b..0000000
--- a/doc/tutorial/kubernetes/quick/cue.mod/gen/k8s.io/apimachinery/pkg/util/intstr/generated.pb_go_gen.cue
+++ /dev/null
@@ -1,14 +0,0 @@
-// Code generated by cue get go. DO NOT EDIT.
-
-//cue:generate cue get go k8s.io/apimachinery/pkg/util/intstr
-
-/*
-	Package intstr is a generated protocol buffer package.
-
-	It is generated from these files:
-		k8s.io/kubernetes/vendor/k8s.io/apimachinery/pkg/util/intstr/generated.proto
-
-	It has these top-level messages:
-		IntOrString
-*/
-package intstr
diff --git a/doc/tutorial/kubernetes/tut_test.go b/doc/tutorial/kubernetes/tut_test.go
index c230d54..601e1c3 100644
--- a/doc/tutorial/kubernetes/tut_test.go
+++ b/doc/tutorial/kubernetes/tut_test.go
@@ -205,16 +205,19 @@
 
 	if *update {
 		// Remove all old cue files.
-		filepath.Walk("", func(path string, info os.FileInfo, err error) error {
+		err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
 			if isCUE(path) {
 				if err := os.Remove(path); err != nil {
 					t.Fatal(err)
 				}
 			}
-			return nil
+			return err
 		})
+		if err != nil {
+			t.Fatal(err)
+		}
 
-		err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
+		err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
 			if isCUE(path) {
 				dst := path[len(dir)+1:]
 				err := os.MkdirAll(filepath.Dir(dst), 0755)
@@ -223,7 +226,7 @@
 				}
 				return copy.File(path, dst)
 			}
-			return nil
+			return err
 		})
 		if err != nil {
 			t.Fatal(err)